Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Form validation PHP and Javascript

There are two very similar in use validation classes, the difference is that one is for javascript: http://www.javascript-coder.com/html-form/javascript-form-validation.phtml and other is for PHP: http://www.html-form-guide.com/php-form/php-form-validation.html

So why to use both PHP and javascript validations? Javascript has immediate response without submitting form, why do we need PHP validation? Answer is simple: because javascript can be disabled. And then user will be able to submit invalid e-mail formats, text into number only fields. So if it is possible - use both PHP and javascript validation. And with given scripts it is very easy.

Here is the sample validation form with both validations:

<?php
if(isset($_POST['register']))
{
    require_once "./formvalidator.php";
    $validator = new FormValidator();
    
    $validator->addValidation("username","req","Username is required");
    $validator->addValidation("username","minlength=4",
                            "Minimal length of user name is 4 symbols");
    $validator->addValidation("username","maxlength=20",
                            "Maximum length of user name is 20 symbols");
    
    $validator->addValidation('email','email', "Incorrect email format");
    
    $validator->addValidation('password', 'req', "Password is required");
    $validator->addValidation('password', 'minlength=5', 
                            "Minimal length of password is 5 symbols");
    $validator->addValidation('password2','req', 
                            "Repeated password is required");
    
    //custom validation class
    class MyValidator extends CustomValidator
    {
        function __construct($er) 
        {
            $this->text = $er;
        }
        
        function DoValidate(&$formars,&$error_hash)
        {
            if($formars['password'] != $formars['password2'])
            {
                $error_hash['password']=$this->text;
                return false;
            }
            return true;
        }
    }
    //adding custom validation
    $custom_validator = new MyValidator("Passwords do not match");
    $validator->AddCustomValidator($custom_validator);
    
    //checking if everything is valid
    if($validator->ValidateForm())
    {
        //ecerything is valid do your thing
    }
    //we got errors, lets output them
    else
    {
        $errormsg = "<B>Errors: </B>";

        $error_hash = $validator->GetErrors();
        foreach($error_hash as $inpname => $inp_err)
        {
            $errormsg = $errormsg."<p>$inp_err</p>n";
        }        
    }
}
?>
<html>
<head>
<script src="/js/gen_validatorv31.js" type="text/javascript"></script>
</head>
<form name='register' action='' method='post'>
<div id='register_errorloc'>
<?php
    //where to output errors
    if (isset($errormsg)){ echo $errormsg."n"; }
?>
</div>
<p>Username: <input type='text' name='username' /></p>
<p>Email: <input type='text' name='email' /></p>
<p>Password: <input type='password' name='password' /></p>
<p>Repeated password: <input type='password' name='password2' /></p>
<input type='hidden' name='register' value='default'/>
<p><input type='submit' value='Register' /></p>
</form>
<script language='JavaScript' type='text/javascript'>
    //creating validator
    //note that value passed here "register", must match form name='register' 
    //and beginning of id of div id='register_errorloc' where to output errors
    var frmvalidator  = new Validator('register');
    //display every error in single box
    frmvalidator.EnableOnPageErrorDisplaySingleBox();
    //display all errors
    frmvalidator.EnableMsgsTogether();
    
    frmvalidator.addValidation("username","req","Username is required");
    frmvalidator.addValidation("username","minlength=4",
                                "Minimal length of user name is 4 symbols");
    frmvalidator.addValidation("username","maxlength=20",
                                "Maximum length of user name is 20 symbol");
                 
    frmvalidator.addValidation('email','email', "Incorrect email format");
    
    frmvalidator.addValidation('password', 'req', "Password is required");
    frmvalidator.addValidation('password', 'minlength=5', 
                                "Minimal length of password is 5 symbols");
    
    frmvalidator.addValidation('password2','req', 
                                "Repeated password is required");
    
    //custom validation function
    function DoCustomValidation()
    {
        var frm = document.forms['register'];
        if(frm.password.value != frm.password2.value)
        {
            sfm_show_error_msg("Passwords do not match", frm.password);
            return false;
        }
        else
        {
            return true;
        }
    }
    //adding custom validation
    frmvalidator.setAddnlValidationFunction('DoCustomValidation');
  
</script>
</html>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus