2

I'm a beginner to javascript. Now, I'm trying to make a form to post back to server. There are some "input" that contains ip address which should be validate before submitting. Now I have done a javascript function which work well. But now I'm trying to add this function into jquery selection. Just confuse how to do it.

This is my validate javascript code.

function ValidateIPaddress(Ipfield)  
 {  
    IpAddr=Ipfield.value;
    var ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;  

    if(!IpAddr.match(ipformat))  
        return true;  
    else
        return false;

 }

and this is now how I implement for this validation.

<input type= "text" name= "LocalIP" style= "margin-right:10px " value="192.168.1.193" class="ip" onfocusout="ValidateIPaddress(document.getElementById('LocalIp'))" id="LocalIp" >            Remote VIP Address :             
                <input type= "text" name= "RemoteVIPAddr" style= "margin-right:10px" value="234.5.6.7" class="ip"  onfocusout="ValidateIPaddress(document.getElementById('RemoteIp'))" id="RemoteIp" >
                Remote VIP Port :             
                <input type= "text" name= "RemoteVIPPort" style= "margin-right:10px" value="5004" class="ip"  onfocusout="ValidatePort(document.getElementById('RemoteVIPPort'))" id="RemoteVIPPort">

Now I want to use jquery selection to always check if there are some invalid input. Which is something like this but with my own design function.

$("input.ip:visible").filter(function() { return this.ValidateIPaddress === true }).addClass("invalid");

Anyone has idea bout it?

5
  • Write a validation function that returns true or false. Then you can use it in both the filter function and in ValidateIPAddress. Commented Aug 16, 2016 at 1:46
  • I updated my question statement Commented Aug 16, 2016 at 1:50
  • This is what I try but it seems not working. Commented Aug 16, 2016 at 1:51
  • this.ValidateIPAddress == true doesn't call the function. Commented Aug 16, 2016 at 1:55
  • And ValidateIPAddress is not a DOM method. Commented Aug 16, 2016 at 1:55

4 Answers 4

2

You're not calling ValidateIPAddress in your filter function, you're just testing whether the DOM element has a non-empty property named ValidateIPAddress. It should be:

$("input.ip:visible").filter(function() {
    return ValidateIPAddress(this);
}).addClass("invalid");
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

isIP(ip) {
  if (typeof(ip) !== 'string')
    return false;
  if (!ip.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)) {
    return false;
  }
  return ip.split('.').filter(octect => octect >= 0 && octect <= 255).length === 4;
}

Original: https://stackoverflow.com/a/50612630/3261332

Comments

1

And if one needs to accept also CIDR format IP/{0-32} please update the 2 lines as below:

if (!ip.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\/([0-9]|[12][0-9]|3[0-2]))?$/)) {
return ip.split('/')[0].split('.').filter(octet => octet >= 0 && octet <= 255).length === 4;

Comments

0

See if this help. This is valid fo IP4 only. 0.0.0.0 - Invalid Any ip with CIDR is invalid

function validateIP(ip) {
    is_valid = false;
    ip = ip.replace(/\s+/, "");

    if(ip.indexOf('/')!=-1){
        alert("IP not valid");
        return false
    }
    
    try {
        var ipb = ip.split('.');
        if (ipb.length == 4) {
            for (i = 0; i < ipb.length; i++) {
                b = parseInt(ipb[i]);    
                if (b >= 0 && b <= 255) {
                    is_valid = true;
                } else {
                    is_valid = false;
                    break;
                }
            }
        }
    } catch (exception) {
        alert("IP is not valid")
        return false;
    }
    if (!is_valid) {
        alert("IP is not valid")
        return false;
    }
    return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.