October 28, 200917 yr Ok so for people who dont wanna really use php for validation which might be considered alittle bit harder imma show u an easy way to do it in javascript below we will use 3 fields name,email,password <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>validation</title> </head> <body> <script language="JavaScript" type="text/javascript"> function validate(){ if(document.form.name.value.length < 1){ alert("Please enter your name"); return false; } if(document.form.email.value.length < 1){ alert("Please enter your email"); return false; } if(document.form.password.value.length < 1){ alert("Please enter your a password"); return false; } } </script> <form name="form" method="POST" action="test.html" onSubmit="return validate();"> <p><b>Name</b><br /><input type="text" name="name" size="30"></p> <p><b>Email</b><br /><input type="text" name="email" size="30"></p> <p><b>Password</b><br /><input type="password" name="password" size="30"></p> <input type="submit" name="submit" value="Submit"> </form> </body> </html> You may also do this as messages instead of pop ups by changing the JavaScript to <script language="JavaScript" type="text/javascript"> function validate(){ if(document.form.name.value.length < 1){ var message ="I am sorry you must fill in your name"; document.write(message); return false; } if(document.form.email.value.length < 1){ var message ="I am sorry you must fill in your email"; document.write(message); return false; } if(document.form.password.value.length < 1){ var message ="I am sorry you must fill in your password"; document.write(message); return false; } } </script> But anyways i do hope this is useful to some people if you don't wanna do it in php but um incase you do i my as well show you that also lol heres the php code to validate using php. <?php $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; $errors = array(); if(isset($_POST['submit'])){ if(!$name){ $errors[] = "I am sorry you must fill in your name"; } if(!$email){ $errors[] = "I am sorry you must fill in your email"; } if(!$name){ $errors[] = "I am sorry you must fill in your password"; } if(count($errors) > 0){ foreach($errors AS $error){ echo "$error <br />"; } }else{ //If no errors do this //If no errors do this } } ?> Ok well as you can see javascript is a little bit easier to do validation well i hope you enjoyed this and find it useful
October 30, 200917 yr Author Great post, I'm sure people will find this very useful No prob thanks for the reply
Create an account or sign in to comment