• Validate کردن فرم
    #1
    Note 
    کدی عالی برای validate کردن فرم

    کد کامل:
    [HTML]
    <!DOCTYPE html>
    <html>
    <!-- Softafzar.net -->
    <head>
    <title>Form Validation</title>
    <meta charset="utf-8" />
    <script type="text/javascript">
    var ck_name = /^[A-Za-z0-9 ]{3,20}$/;
    var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    var ck_username = /^[A-Za-z0-9_]{1,20}$/;
    var ck_password = /^[A-Za-z0-9!@#$%^&*()_]{6,20}$/;

    function validate(form){
    var name = form.name.value;
    var email = form.email.value;
    var username = form.username.value;
    var password = form.password.value;
    var errors = [];

    if (!ck_name.test(name)) {
    errors[errors.length] = "Enter Correct Name .";
    }

    if (!ck_email.test(email)) {
    errors[errors.length] = "You must enter a valid email address.";
    }

    if (!ck_username.test(username)) {
    errors[errors.length] = "You valid UserName no special char .";
    }

    if (!ck_password.test(password)) {
    errors[errors.length] = "You must enter a valid Password ";
    }

    if (errors.length > 0) {

    reportErrors(errors);
    return false;
    }
    return true;
    }
    function reportErrors(errors){
    var msg = "Please Enter Valide Data...\n";
    for (var i = 0; i<errors.length; i++) {
    var numError = i + 1;
    msg += "\n" + numError + ". " + errors[i];
    }
    alert(msg);
    }
    </script>
    </head>
    <body>
    <form method="post" action="a.php" onSubmit="return validate(this);" name="form">
    <input type="text" id="name" name="name" value="" placeholder="Enter your name" />
    <input type="email" id="email" name="email" value="" placeholder="Enter your e-mail address" />
    <input type="text" id="username" name="username" value="" placeholder="Enter username" />
    <input type="password" id="password" name="password" value="" placeholder="Enter password" />
    <input type="submit" />
    </form>

    </body>
    </html>
    [/HTML]

    پاسخ
    ایجاد موضوع جدید   پاسخ به موضوع  

    کاربرانِ درحال بازدید از این موضوع:   1 مهمان