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!
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....
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
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