Form Validation Using Jquery Examples - HTML

Form Validation Using Jquery Examples - HTML


Corrected script.js:

$(function() { $("#username_error_message").hide(); $("#email_error_message").hide(); $("#phone_message").hide(); $("#company_error_message").hide(); $("#comment_error_message").hide(); var error_username = false; var error_email = false; var error_phone = false; var error_company = false; var error_comment = false; $("#form_username").focusout(function() { check_username(); }); $("#form_email").focusout(function() { check_email(); }); $("#form_phone").focusout(function() { check_phone(); }); $("#form_company").focusout(function() { check_company(); }); $("#form_comment").focusout(function() { check_comment(); }); function check_username() { var username_length = $("#form_username").val().length; if (username_length < 5 || username_length > 20) { $("#username_error_message").html("name 5-20 characters"); $("#username_error_message").show(); error_username = true; } else { $("#username_error_message").hide(); } } function check_email() { var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i); if (pattern.test($("#form_email").val())) { $("#email_error_message").hide(); } else { $("#email_error_message").html("Invalid email address"); $("#email_error_message").show(); error_email = true; } } function check_phone() { var phone_length = $("#form_phone").val().length; if (phone_length < 8) { $("#phone_error_message").html("phone 9-10 characters"); $("#phone_error_message").show(); error_phone = true; } else { $("#phone_error_message").hide(); } } function check_company() { var company_length = $("#form_company").val().length; if (company_length < 5 || company_length > 20) { $("#company_error_message").html("Company name 5-20 characters"); $("#company_error_message").show(); error_company = true; } else { $("#company_error_message").hide(); } } function check_comment() { var comment_length = $("#form_comment").val().length; if (comment_length < 10 || comment_length > 20) { // Corrected 'coomment_length' to 'comment_length' $("#comment_error_message").html("Should be between 10-20 characters"); $("#comment_error_message").show(); error_comment = true; } else { $("#comment_error_message").hide(); } } $("#registration_form").submit(function() { error_username = false; error_email = false; error_phone = false; error_company = false; error_comment = false; check_username(); check_email(); check_phone(); check_company(); check_comment(); if (error_username == false && error_email == false && error_phone == false && error_company == false && error_comment == false) { return true; } else { return false; } }); });

Fixes made:

  1. Fixed typo in check_comment function (changed coomment_length to comment_length).

  2. Ensured the form submission check ($("#registration_form").submit()) properly checks all validation errors.

Once these fixes are applied, the form should work correctly without validation errors. Let me know if you need further adjustments!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

1 Comments

CAN FEEDBACK
  1. laxmicynixit
    laxmicynixit
    Thank you for sharing wonderful information with us to get some idea about it.
    Oracle OSB Training
    OSB Online Training Hyderabad
    osb certification training
    soa training
    oracle soa 12c training
    tableau online training

close