2012-11-13 109 views
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 

回答

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'); 
} 

參考:Regular Expressions Field Validation

+0

非常感謝您支那 –