validate one IP address from two using jquery validator

Discussion in 'jQuery' started by kamalmajid, Jan 29, 2014.

  1. #1
    I have a form that has text fields for the user to type one or more text field and proceed to search, two text fields are for ip address :


    
    <label class="formLabel">IP Address</label>
    <input type="text" property="valueApiAddress" styleClass="value" tabindex="9"/>
    <br/>
    
    <label class="formLabel">External IP Address</label>
    <input type="text" property="valueExternalApiAddress" styleClass="value" tabindex="10"/>
    <br/>
    
    HTML:
    I am using the jquery validator, but when the user want to search for results that only match IP Addrese, the other field for External IP Address show validation error, ho to show the validation error only for the field of IP that was filled ?

    jQuery.validator.addMethod("ipAddressFormat",function(value, element){
        theName = "IPaddress";
        var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
        var ipArray = value.match(ipPattern);
        if (value == "0.0.0.0" || value == "255.255.255.255" ||
            value == "10.1.0.0" || value == "10.1.0.255" || ipArray == null)
            return false;
        else {
            for (i = 0; i < 4; i++) {
                thisSegment = ipArray[i];
                if (thisSegment > 254) {
                    return false;               
                }
                if ((i == 0) && (thisSegment > 254)) {
                    return false;
                }
            }
        }
        return true;   
    }, "Invalid IP Address");
    
    
    
    
    $(document).ready(function() {   
        $("#agentsSearchForm").validate({
            rules: {
                valueApiAddress: {
                    ipAddressFormat: true              
                },
                valueExternalApiAddress: { 
                    ipAddressFormat: true               
                },
                valuePort: {
                    range: [1, 65535]               
                } 
            },
             messages: {   
                valuePort: {
                    range: "Valid port range is 1 to 65535."
                } 
            } 
        });
    
    });
    
    Code (markup):

     
    kamalmajid, Jan 29, 2014 IP