PHP Basic Form validation problems V2

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]

<?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]

Syntax of inputs are incorrect

Change :

Name:<input type=“input” name=“name” value=""

To

Name:

Do this with all inputs.

After all, the comparation equal are (==) not (=), change this.

Another error was that you can’t use variable of a function, you must return this value.

One question, why are you using enctype=“multipart/form-data” in form, it’s not required. It’s not incorrect, but use this when it must required, when use input=file for example.

Try with :
[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;
}
return $validemail;
}
//validates the account number format
function isAccount(){
if(preg_match("/^\d{8}$/", $account))
{
$validaccount= true;
}
else
{
$validaccount= false;
}
return $validaccount;
}

//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.”;
}

//validates email
$validemail = isEmail($email);
if ($validemail==false){
echo “-Email is invalid”;
}

//validates account
$validaccount =isAccount($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:
        <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 />
[/php]

Thanks for the help… I’ve almost got everything working… I’m always stumbling on syntax!

If all my inputs are valid- name, email, and account- my final confirmation Hello “name” your account is xxx
is not printing?

I’m also still trying to work out:
The Account Number can only contain numbers and optionally ‘-‘. A valid account number contains exact 8 numbers when ‘-‘ is ignored
I have it set to validate on 8 numbers but I’m not sure how to deal with dashes?

Thanks for all your help!

[php]

<?php $name = $_POST['name']; $email = $_POST['email']; $account = $_POST['account']; $validaccount=''; $validemail=''; //validates the email address format function isEmail(){ $validemail=''; if (preg_match("/^(\w+((-\w+)|(\w.\w+))*)\@(\w+((\.|-)\w+)*\.\w+$)/",$email = $_POST['email'])) { $validemail== true; } else { $validemail== false; echo "
-Email is invalid.
"; } return $validemail; } //validates the account number format function isAccount(){ $validaccount=''; if(preg_match("/^\d{8}$/", $account = $_POST['account'])) { $validaccount== true; } else { $validaccount== false; echo "
-Account number is invalid.
"; } return $validaccount; } //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 isAccount($account);//validates account //Confirmation after all inputs are verified and valid if ($name && $validemail==true && $validaccount==true){ echo "
Hello ". $name . " Your account number is " . $account . ".
"; } ?>
Name:
        <br> Email:<input type="input" name="email" value="<?php echo htmlentities($_POST['email']);?>"><br /> 
   
        <br> Account Number:<input type="input" name="account" value="<?php echo htmlentities($_POST['account']);?>"><br />
      
        <br> <input type="submit" name="goto" value="Submit"/><br />
		</form>
[/php]

[php]

<?php ?>
    <form action="process.php" method="POST">
        <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]

To match the account number you can strip out everything that isnt a number, then check the string length:

[php]
$noDashAccount = preg_replace(’/[^\d]+/’, ‘’, $accountNumber);
if(strlen($noDashAccount) == 8)
{
$validAccount = true;
}
[/php]

Or, you can strip all the dashes, then verify that you have 8 numbers.
[php]
$noDashAccount = preg_replace(’/[-]+/’, ‘’, $accountNumber);
if(preg_match("/^\d{8}$/", $account))
{
$validAccount = true;
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service