jQuery - Form validation

0 votes
247 views
added Jun 19, 2018 in jQuery by LC Marshal Captain (25,790 points)
edited Aug 29, 2018 by LC Marshal
$('#outlook-form-submit').click(function(){
    var selectRequest = $('#outlook-form-select-request option:selected').val(),
        name = $('#outlook-form-name').val(),
        email = $('#outlook-form-email').val(),
        contact = $('#outlook-form-contact').val(),
        budget = $('#outlook-form-budget').val();
        requirement = $('#outlook-form-requirement').val();

    var  submitUrl = '';
    submitUrl = "/erp_report_queue/add";
    submitUrl += "&select_request="+selectRequest;
    submitUrl += "&name="+name;
    submitUrl += "&email="+email;
    submitUrl += "&contact="+contact;
    submitUrl += "&budget="+ budget;
    submitUrl += "&requirement"+requirement;
    //condition when variable null or empty
    if (selectRequest===null || selectRequest==="") {  
        alert("Please choose the type of request");
        return false;

    } else if (name===null || name==="") {   
        alert("Your name can't be empty");  
        return false; 

    } else if (email===null || email==="") {
        alert("Your email can't be empty");
        return false;

    } else if (budget===null || budget==="") {
        alert("Your budget can't be empty");
        return false;

    } else if (validateEmail(email)) {

    } else if (contact===null || contact==="") {
        alert("Your contact number can't be empty");
        return false;

    } else if (validateContact(contact)) {

    } else {
        alert("Please enter a valid email address  (e.g. [email protected])");
        return false; 
    }
    //validate email
    function validateEmail(email) {
        var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        if (filter.test(email)) {
            return true;
        }
        else {
            return false;
        }
    }
    //validate contact number
    function validateContact(contact) {
        var filterContact = /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/;
        if (filterContact.test(contact)) {
            return true;
        }
        else {
            return false;
        }
    }

    console.log(submitUrl);
    $.getJSON(submitUrl,
      function(data) {
        if (data == 'success'){
          console.log("redirecting..");
          window.location.replace("/");
        }
      }
    );
});
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...