0

I have coded not empty on fields. how do i check that email is valid or either mobile number is valid one that too us phone number.

if(mob_or_email==""){

                document.getElementById('busp_email').innerHTML="Mobile/Email required";    
                $("#busp_email").removeClass('field_validation_error hidden');
                $("#busp_email").addClass('field_validation_error');
                $("#busi_name").css("color","#f42156");

    }

    if($('#login_password').val()==""){

                document.getElementById('logp_pwd').innerHTML="Password required";  
                $("#logp_pwd").removeClass('field_validation_error hidden');
                $("#logp_pwd").addClass('field_validation_error');
                $("#log_pwd").css("color","#f42156");
    }
Tamilselvan v
  • 25
  • 1
  • 7
  • Where and how do you set "mob_or_email" variable? – Jkike Sep 03 '15 at 09:42
  • 1
    If you're only interested in HTML5 browsers you can apply a regex pattern to the pattern attribute on the input element, which you might find easier than doing it in JS. – Andy Sep 03 '15 at 09:46
  • possible duplicate of [Validate email address in JavaScript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Jon Saw Sep 03 '15 at 09:50

1 Answers1

0

If I understand your request correctly, you may have an e-mail or a phone number in the same field?

You will need regular expressions for this (If my suggestions are not satisfying, search trough the internet, there are many samples for specific cases)

function validateEmail(email) {
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return re.test(email);
}
function validatePhone(phone) {
    var re = /^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/;
    return re.test(phone);
}

With these functions you can validate e-mail and us phone numbers. just use

if (validateEmail(mob_or_email) or validatePhone(mob_or_email)) {
  //is either a valid email or (us) phone number
}
Xavjer
  • 8,838
  • 2
  • 22
  • 42