Friday, 1 July 2016

Multiple Field Validation Check Using JavaScript

Keep it in a function
Get values from the fields and Check with boolean values

try{
        var spName = $('#sponsorName').val();
        var spCode = $('#sponsorCode').val();
        var psContactName = $('#primarySponsorContactName').val();
        var psContactNumber = $('#primarySponsorContactNumber').val();
        var psEmail = $('#primarySponsorEmail').val();
        var ssEmail = $('#secondarySponsorEmail').val();
        var ssContactNumber = $('#secondarySponsorContactNumber').val();
        var valid = true;



      
        if (spName) {
            $("#sponsorNameId").css("display", "none");
            $("#sponsorName").parent().removeClass("has-error");
           
              
            }
            else {
                $("#sponsorNameId").show();
                $("#sponsorNameId").text(errSponsorNameRequired);
                $("#sponsorName").parent().addClass("has-error");
                valid = false;
            }
       

        if (spCode) {
           

            $("#sponsorCodeId").css("display", "none");
            $("#sponsorCode").parent().removeClass("has-error");
           

            }
        else {
            $("#sponsorCodeId").show();
            $("#sponsorCodeId").text(errSponsorCodeRequired);
            $("#sponsorCode").parent().addClass("has-error");
            valid = false;
            }

       
        if (psContactName) {


            $("#primarySponsorContactNameId").css("display", "none");
            $("#primarySponsorContactName").parent().removeClass("has-error");
           
               
            }
            else {
                $("#primarySponsorContactNameId").show();
                $("#primarySponsorContactNameId").text(errPrimarySponsorContactNameRequired);
                $("#primarySponsorContactName").parent().addClass("has-error");
                valid = false;
            }

       
        var numberValid = /^(?=.*[0-9])[- +()0-9]+$/;
        if (psContactNumber && psContactNumber.match(numberValid))
        {
          
                $("#primarySponsorContactNumberId").css("display","none");
                $("#primarySponsorContactNumber").parent().removeClass("has-error");
             
        }
        else {

            $("#primarySponsorContactNumberId").show();
            $("#primarySponsorContactNumberId").text(errPrimarySponsorContactNumberRequired);
            $("#primarySponsorContactNumber").parent().addClass("has-error");
            valid = false;
        }

        var emailvalid = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;



        if (psEmail && psEmail.match(emailvalid)) {

         
            $("#primarySponsorEmailId").css("display", "none");
                $("#primarySponsorEmail").parent().removeClass("has-error");
              
         
           
        }
        else {
            $("#primarySponsorEmailId").show();
            $("#primarySponsorEmailId").text(errPrimarySponsorEmailRequired);
            $("#primarySponsorEmail").parent().addClass("has-error");
            valid = false;
        }
       

        if (ssEmail) {

            if (ssEmail.match(emailvalid)) {
                $("#secondarySponsorEmailId").css("display", "none");
                $("#secondarySponsorEmail").parent().removeClass("has-error");
              
            }
            else
            {
                $("#secondarySponsorEmailId").show();
                $("#secondarySponsorEmailId").text(errSecondarySponsorEmail);
                        $("#secondarySponsorEmail").parent().addClass("has-error");
                        valid = false;
            }
        }
       
      

        if (ssContactNumber) {

            if (ssContactNumber.match(numberValid)) {
                $("#secondarySponsorContactNumberId").css("display", "none");
                $("#secondarySponsorContactNumber").parent().removeClass("has-error");
              
            }
            else {

                $("#secondarySponsorContactNumberId").show();
                $("#secondarySponsorContactNumberId").text(errSecondarySponsorContactNumber);
                $("#secondarySponsorContactNumber").parent().addClass("has-error");
                valid = false;
            }
        }
        return valid;
    }
    catch (errorMsg) {
        console.log(errorMsg.message);
    }

No comments:

Post a Comment