Moderator: Please delete my old post if you see it… I didn’t notice the registration requirement. Thanks!
Greetings:
I’m having problems setting up my first very simple web form validation. Currently, I’m having problems getting it to validate the email address and account number. I think I’m not passing the info to the functions inEmail and isAccount correctly. I’m sorry I’m very new to programming.
I also want the errors to all show up above the form like so:
Name is invalid
Email is invalid
Account is invalid
(Copy of form below)
I’m also trying to validate that the Account Number can only contain numbers and optionally ‘-‘. A valid account number contains exact 8 numbers when ‘-‘ is ignored- I’m a little clueless to get this logic to work–is there a function that ignores dashes in a string of numbers?
Thanks for any guidance!
Here’s my code so far:
[php]
<form action="process.php" method="POST" enctype="multipart/form-data">
<br> Name:<input type="input" name="name" value=""<br />
<br> Email:<input type="input" name="email" value=""<br />
<br> Account Number:<input type="input" name="account" value=""<br />
<br> <input type="submit" name="goto" value="Submit"/><br />
</form>
</body>
[/php]
[php]
<?php $name = $_POST['name']; $email = $_POST['email']; $account = $_POST['account']; $validaccount=''; $validemail=''; //validates the email address format function isEmail(){ if (preg_match("/^(\w+((-\w+)|(\w.\w+))*)\@(\w+((\.|-)\w+)*\.\w+$)/",$email)) { $validemail= true; } else { $validemail= false; }} //validates the account number format function isAccount(){ if(preg_match("/^\d{8}$/", $account)) { $validaccount= true; } else { $validaccount= false; } } //validates the form if (empty($name) && empty($email) && empty($account)){ echo "Your form is blank. Please provide information.";//checks to see if form is blank } elseif (empty($name)){ echo "-Name is blank."; } isEmail($email);//validates email if ($validemail=false){ echo "-Email is invalid"; } isAccount($account);//validates account if ($validaccount=false){ echo "-Account number is invalid."; } //Confirmation after all inputs are verified and valid if ($name && $validemail=true && $validaccount=true){ echo "Hello ". $name . " Your account number is " . $account . ".
"; } ?>
Name:<input type="input" name="name" value=""
<br> Email:<input type="input" name="email" value=""<br />
<br> Account Number:<input type="input" name="account" value=""<br />
<br> <input type="submit" name="goto" value="Submit"/><br />
</form>
[/php]