0

I have a Forgot Password form where we have a single input field in which the user can put either the email or the phone number.

Now my validation should work on based on the user content. If the user puts an email, the email validation should run and if the user puts mobile number, the mobile number validation should run.

Can someone please help in such a scenario. Tried google-ing but could not find any such scenario.

Nitin Suri
  • 960
  • 1
  • 8
  • 20
  • no,this is ceratainly not possible...you may have the user select a radio button ..and depending on that you ca provide your respective validation!!!! – HIRA THAKUR Aug 05 '13 at 07:07

2 Answers2

1
var value=document.getElementById(id).value;
var email = /^[a-zA-Z\._-]+@[a-zA-Z\.-]+\.[a-z]{2,6}$/;
var phone = /^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}98(\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$/;
if(email.test(value)) {
//email
} else if (phone.test(value)) {
//phone #
} else {
//invalid
}

Try that. Since the phone # regex is kinda complicated, use http://regexlib.com/ to find a simpler one.

0

You could write a regex that check if the input is a phone number / e-mail. But if the user inputs something wrong, you might have that regex fail. Here's a regex for phone numbers: example of a regular expression in jquery for phone numbers, if the input matches this regex, use the phone validation. If not, use the e-mail validation.

Community
  • 1
  • 1
Tdelang
  • 1,298
  • 2
  • 12
  • 20