williamrouse Posted March 27, 2010 Report Share Posted March 27, 2010 I am looking for a correct way to test a JavaScript regular expression for phone number and email address. This includes the RegExp string and the syntax. First the syntax: I am confused about using the quote mark (“) in defing the RegExp. Is it var email_test = new RegExp(/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/); or var email_test = new RegExp(“/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/”); Secondly, what RegExp have you used that works well for phone and email. These are the ones I found for phone and email var email_test = new RegExp(/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/); var phone_test = new RegExp(^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$); Thanks! Quote Link to comment Share on other sites More sharing options...
williamrouse Posted March 27, 2010 Author Report Share Posted March 27, 2010 From what I have read on the web I would think that these two chunks of code would work, but they don’t. Is this the general way you would define and test a RegExp object? Can you give me some ideas of how to test for phone numbers and email addresses? // Test for a vaild email address var email = $('input#email').val(); var email_test = new RegExp('\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'); if (!email_test.test(email)) { $('#statusBox').html('<div class="error">I think your email is wrong...</div>'); return false; } // Test for a vaild phone number var phone = $('input#phone').val(); var phone_test = new RegExp('^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$'); if (!phone_test.test(phone)) { $('#statusBox').html('<div class="error">I think your phone number is incorrect</div>'); return false; } Quote Link to comment Share on other sites More sharing options...
administrator Posted March 27, 2010 Report Share Posted March 27, 2010 For email addresses, I've found that it is a bad idea to go too complex with the pattern simply because the domain extensions are constantly changing - one day we may have a .zipper ... if you follow what I mean. So, with that in mind, I would suggest that you just test for two things: The @ symbol A period: . In the end, what you are trying to do is test that the user didn't accidentally make a mistake, so creating a second text box for email confirmation is the best approach. I say this, because if a user doesn't want to put in their real email address, there is nothing you can do about it. I would think for phone numbers, much the same applies, as different regions of the world have different patterns ... to research all the patterns would be a pain and really in the end, it is of dubious advantage to test for them, just as it is for email addresses. Sometimes in programming, tactical considerations are the wiser course ... rather than technical implementations. I hope that makes sense. Stefan Quote Link to comment Share on other sites More sharing options...
williamrouse Posted March 27, 2010 Author Report Share Posted March 27, 2010 Stephan: Apparently I did not set email notification and I researched my question on the net for a long time today and found a solution. All in all I take your thoughts are appropriate for what I have in mind, especially for phone number. The email is more important in my case since that would be the primary method of contact and the notion of a confirmation email text box sounds like an adequate solution. At any rate here is a JavaScript solution for both email and phone. The phone as be minimally tested for validation of US phone numbers. // Test for a vaild email address var email_test = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/; if ( !email_test(email)){ $('#statusBox').html('<div class="error">I think your email is wrong...</div>'); return false; } // Test for a vaild phone number var phone_test = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/; if (!phone_test.test(phone)) { $('#statusBox').html('<div class="error">I think your phone number is incorrect</div>'); return false; } Quote Link to comment Share on other sites More sharing options...
administrator Posted March 28, 2010 Report Share Posted March 28, 2010 Thanks for posting your solution - it is good to close up the discussion with an answer. Stefan Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.