Click to See Complete Forum and Search --> : Help with regular expressions
savoie
10-22-2004, 10:28 AM
I have a form validator that has the following code.
var tfld = trim(vfld.value); // value of field with whitespace trimmed off
var telnr = /^\+?[0-9 ()-]+[0-9]$/
if (!telnr.test(tfld)) {
msg (ifld, "error", "ERROR: permitted (digits,space,(),+)");
vfld.focus();
return false;
}
Can anyone help me adjust the regexp so it wont trigger if the field I pass contains
'(xxx)xxx-xxxx'
Thanks.
baconbutty
10-22-2004, 11:45 AM
Some amateur thoughts:-
Your question is perhaps a little ambiguous. What do you mean by "won't tigger"?
Do you mean that:-
if (!telnr.test(tfld))
{
// NOT A NUMBER ERROR
}
should fire the ERROR for '(xxx)xxx-xxxx' or NOT?
The reason I ask, is that if mean that the error will NOT be triggered for '(xxx)xxx-xxxx' then that already is the case, as the following alerts true for me:-
function fTest()
{
var sTest="(123)456-7890";
var telnr = /^\+?[0-9 ()-]+[0-9]$/
window.alert(telnr.test(sTest)); //<-- Alterts "true" in IE6/Mozilla
window.alert(!telnr.test(sTest)); //<-- Alterts "false" in IE6/Mozilla
}
Could you refine the question?
savoie
10-22-2004, 11:55 AM
As the script is currently written it returns 'false' if the field contains anything besides (0-9,(),-,+). I want to add the string '(xxx)xxx-xxxx' as allowable. So n the end it will allow '(xxx)xxx-xxxx' to pass validation in conjunction with the original (0-9,(),-,+)
Does this help?
thanks,
Willy Duitt
10-22-2004, 12:04 PM
Then include the X in your expresion along with 0-9()+
But that whole thing does not make sense from where I am sitting because: (12345)123(4)56-1234+++5678 would appear to pass...
There are many phone number validation scripts around... I'm sure a search of this forum will turn up another which is much better than what you are using...
Or, you can try to explicitly describe what formats will and will not pass and someone could write you another but as it is.... Your question is not clear...
.....Willy
HaganeNoKokoro
10-22-2004, 12:06 PM
So, just so I get this straight, you want this string "(xxx)xxx-xxxx" to pass validation, where the x's are actually "x", not meant to represent generic digits? I only ask because it will already pass if the x's are 0-9, ( ) or -. In this case, you could use
/^\+?[0-9 ()-x]+[0-9x]$/
This question does not really relate to your question, but is noteworthy nonetheless: what sort of trim function are you using? Typically trimming removes only leading and trailing whitespace, not the stuff inside the string. To remove all whitespace in a string, usestring=string.replace(/[\s]/g, "");
Another concern: your current regexp will allow things like "(-5" to pass validation; all it does is check that there are parentheses, dashes, and numbers ending with a number.
If you're looking for a regexp to make sure the phone number format is valid (at least, for North America style phone numbers)
/^([\+]?\([\d]{3}\)|[\d]{3})?[\d]{3}[\-]?[\d]{4}$/
tests true for
1234567
123-4567
1231234567
123123-4567
(123)1234567
(123)123-4567
+1231234567
+123123-4567
+(123)1234567
+(123)123-4567
baconbutty
10-22-2004, 12:11 PM
Try one of these:-
function fTest()
{
var sTest1="(xxx)xxx-xxxx";
var sTest2="(123)456-6890";
var telnr1=/^\+?\([x0-9]{3}\)[x0-9]{3}-[x0-9]{4}$/g; // STRICT
var telnr2=/^\+?[x0-9 ()-]+[x0-9]$/; // FLEXIBLE
window.alert(telnr1.test(sTest1));
window.alert(telnr2.test(sTest2));
window.alert(telnr2.test(sTest1));
window.alert(telnr2.test(sTest2));
var telnr3=/^\+?[x0-9 ()-]+?[x0-9]$/g; //<-- Odd returns false when "g" switch is added unless "+?" is used in the middle
window.alert(telnr3.test(sTest1));
}
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsgrpregexpsyntax.asp
savoie
10-22-2004, 12:29 PM
yes I want to pass (xxx)xxx-xxxx litteraly, the 'x's represent the letter x, not an arbitrary number. I am pre-populating the phone fields with (xxx)xxx-xxxx so the user knows the format I want. If the user doesn't enter a value I need (xxx)xxx-xxxx not to get caught in validation.
I'm up for any enhancements to the expression, so if
/^([\+]?\([\d]{3}\)|[\d]{3})?[\d]{3}[\-]?[\d]{4}$/
works better than my original test I'm all for it as long as I can get my '(xxx)xxx-xxxx' in the mix?? What would the new expression look like.
Anyone have a good resource on how to understand and build regexpr?
Thanks for all your input folks!
baconbutty
10-22-2004, 03:03 PM
See my original suggestions:-
var telnr1=/^\+?\([x0-9]{3}\)[x0-9]{3}-[x0-9]{4}$/g; // STRICT
This produces true for (xxx)xxx-xxxx, STRICT
var telnr2=/^\+?[x0-9 ()-]+[x0-9]$/; // FLEXIBLE
This produces true for (xxx)xxx-xxxx, FLEXIBLE (i.e. no requirement for () and -
See my previous post for a syntax link.
7stud
10-23-2004, 04:06 AM
Hi,
You can always just test for the string itself:
if( !telnr.test(tfld) && !telnr.test(/"(xxx)xxx-xxxx"/) )
Anyone have a good resource on how to understand and build regexpr?
Tutorial: http://www.sitepoint.com/article/expressions-javascript
A library of Regex's for matching all kinds of things: http://www.regexlib.com/DisplayPatterns.aspx
Regex website: http://www.regular-expressions.com/
Books: Regular Expressions Pocket Reference http://www.amazon.com/exec/obidos/ASIN/059600415X/qid=1098523113/sr=2-1/ref=pd_ka_b_2_1/104-3339006-7222344
savoie
10-27-2004, 05:21 PM
I'm using the expression
/^([\+]?\([\d]{3}\)|[\d]{3})?[\d]{3}[\-]?[\d]{4}$/
as per the above posts recomondation.
It handles all the phone number formats I need with the exception of the following
123-456-7890
Can someone tweak it so it works with this above format too?
Thanks!!
7stud
10-27-2004, 05:53 PM
I'd bet you never went to the regex library link.
javaNoobie
10-27-2004, 11:54 PM
String.prototype.isPhone = function(){return /^([\+]?\(?[\d]{3}\)?|[\d]{3}\-?)?[\d]{3}[\-]?[\d]{4}$/.test(this);}
alert('123-456-7890 returns ' + '123-456-7890'.isPhone())
alert('(123)-456-7890 returns ' + '(123)-456-7890'.isPhone())
alert('(123)456-7890 returns ' + '(123)456-7890'.isPhone())
alert('+123-456-7890 returns ' + '+123-456-7890'.isPhone())
alert('+(123)-456-7890 returns ' + '+(123)-456-7890'.isPhone())
alert('+(123)456-7890 returns ' + '+(123)456-7890'.isPhone())
savoie
10-28-2004, 08:49 AM
Thanks for the tweak,
And yes '7', I did visit the expression resources listed. But there are so many options (and I am just starting to understand them) that I didn't have time to try each one out to verify it was what I needed. The expression on the above post with javanoobie's tweak is exactly what the Dr. ordred.
Thanks everyone for your help!!!!!!!!!!