-
form validation
Hey guys.
Is there a javascript or other serverside script out there that will allow me to check and see if a required form element has a certain value?
Here is my current code, it might give you a lead to what I mean:
HTML Code:
<form action="netxpage.php">
ID: <input type="text" name="id"><br>
// This field requires the validation, it must have a certain value in it for the
//user to procede.
<input type="submit" value="Continue">
</form>
-
yes of course. would you prefer it javascript(client) or php(server) (i would choose php, almost impossible to trick)? what do you want the syntax to be? e.g. xxx-xxx-xxx or xxxxxxxxx or only letters or only numbers or a combination of both?
-
How about php with a santax of xxxxxxxxx and only #'s.
-
Edit: script changed
your form(must be .php):
HTML Code:
<form action="validate.php" method="post">
<div style="color: red"><?php if ($error) { echo $error; } ?></div>
ID: <input type="text" name="id"><br>
<input type="submit" value="Continue">
</form>
validate.php:
PHP Code:
<?php
$nextpage=""; //the page to go to if there are no errors e.g. nextpage.php
$formpage=""; //the form page
$string = $_POST['id'];
if ($string==null) {
$error = "You did not enter anything.";
} else {
if (!eregi('^[0-9]{9}$', $string)) {
$error = "'$string' is not a valid id.";
}
}
if ($error == null) {
echo "<script type=\"text/javascript\">
window.location=\"".$nextpage."\";
</script>
<p>If you were not redirected click <a href=\"".$nextpage."\">here.</a></p>";
} else {
include($formpage);
}
?>
-
Tks!
Uh.. where does the valid id get entered in the script?
-
do you mean where the script gets the content of the ip input, or the form?
-
the id that has to match the one the user enters
-
i think i misinterpreted your fist post. you wanted to use the id to login?
-
Does he just want something like:
Code:
$id=$_POST['id'];
if ($id=="1232343") {
print("
<head>
<script type=\"javascript\">
window.location='nextpage.html';
</script>
<body>
</body>");
}
else {
print("
<head>
<script type=\"javascript\">
setTimeOut("window.location='javascript.go:history(-1)'",5000);
</script>
<body>
ID wrong, please try again.
</body>
}
Then a form as so:
Code:
<form action="thephp.php" method="post">
<input type="text" name="id" value="Type ID in here.">
<input type="submit" value="Check ID">
</form>
That is very simple, should work just fine for 1, 2, 3, up to maybe 6 IDs but if more then you shouldn't use this method most likely.
Also someone had best check to make sure the "javascript.go:history(-1) is right, I believe it is but I can't be sure.
Tim
-
Hi Tim
Tks for the script, but there seems to be a error in line 18.
Here is the error code:
Parse error: syntax error, unexpected T_STRING in /***/****/***/***/***/thephp.php on line 18