0

我有一个用户注册表。我已经使用验证来检查是否所有字段都已填写。我需要使用 javascript 验证电子邮件和手机号码,如果它无效,则使用警报框发出警报。请帮忙

代码是

            var name = $("#name").val();
            var inst = $("#inst").val();
            var email = $("#email").val();
            var mobile = $("#mobile").val();
            var dataString = 'name='+ name + '&inst='+ inst + '&email='+ email +'&mobile='+ mobile;

            if(name==''|| inst=='' || email=='' || mobile=='')
            {
            alert("Please Enter all the Fields");
            }
else 
{ //rest of code comes here
4

1 回答 1

1
var emailRe = /^\w+([\.\-]?\w+)*@\w+([\.\-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/;
var phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
var email_address = $('#email').val();
var phone_number = $('#phone').val();

if(emailRe.test(email_address) === false){
  alert(email_address + ' is invalid');
}

if(phoneRe.test(phone_number) === false){
  alert(phone_number + ' is invalid');
}

参考:正则表达式字段验证

于 2012-11-13T11:33:45.087 回答