Regular Expression for IP address won't work with RegEx(param), only works with ...

Discussion in 'JavaScript' started by maximusdm, Apr 5, 2010.

  1. #1
    Hi all,

    I dont know why but tests reveal that if I use "variable.value" and pass that info to the valGlobal below, the regular expressions won't work! what can I do to dix this?

    if (globalValueControl.value != "" && itemValueTypeControl.value != "" )
    {
    //var valGlobal = new RegExp(itemValueTypeRegExControl.value); // it does not validate the value properly
    //var valGlobal = itemValueTypeRegExControl.value; // Object does not suppot this property or method
    //var valGlobal = new RegExp(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/); // it works fine
    var valGlobal = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; // it works fine

    if (!valGlobal.test(globalValueControl.value))
    {
    alert("Test Failed! Global value: " + itemValueTypeErrorControl.value);
    formIsValid=false;
    }
    }

    Thank you!
     
    maximusdm, Apr 5, 2010 IP
  2. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How is this a regex for IP address? It fails completely with my own IP (2001:5c0:a043:5ae0:3::xxxx)
     
    krsix, Apr 5, 2010 IP
  3. maximusdm

    maximusdm Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Anybody please knows why the constructor RegEx WILL NOT accept a variable.value ? it only works when I type the pattern in it parameter.

    @Krsix... not sure, it works for me: 10.10.10.10, 192.1.1.1, etc....
     
    maximusdm, Apr 5, 2010 IP
  4. unigogo

    unigogo Peon

    Messages:
    286
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Your regexp contains backslash (\) character. Try this javascript in your browser to help understand.

    var str = "aaa\bbb"; 
    alert(str);
    
    Code (markup):
    You don't see the backslash \ in the string. The backslash must be quoted as \\ . Try,

    var str = "aaa\\bbb"; 
    alert(str);
    Code (markup):
    Now you get what you want. So you have to quote the string that has \ in it before process regexp. You can use the following method the quote the string.

    String.prototype.quote = function() {
        return this.replace(/([\\])/g,"$1$1");
    }
    
    Code (markup):
    So try,

    var valGlobal = new RegExp(itemValueTypeRegExControl.value.quote());
    Code (markup):
    Yu can test you regexp in the online regexp tester
     
    unigogo, Apr 10, 2010 IP
  5. maximusdm

    maximusdm Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for replying. I've read somewhere about the issue and had tried that before.
    By using alert(string) I am able to see the string with the extras "\" however the RegEx will still fail every time I type in a IP address :(
     
    maximusdm, Apr 12, 2010 IP