2011-08-04 72 views
0

林验证表单,但即时通讯努力得到它仅接受名字和姓氏字段验证只字母输入

希望字母形式u能帮助

继承人我的代码:

$(document).ready(function(){ 
// Place ID's of all required fields here. 
required = ["firstname", "lastname", "email"]; 
// If using an ID other than #email or #error then replace it here 
email = $("#email"); 
errornotice = $("#error"); 
// The text to show up within a field when it is incorrect 
emptyerror = "Please fill out this field."; 
emailerror = "Please enter a valid e-mail."; 
onlyletters = "Only letters allowed."; 

$("#theform").submit(function(){  
    //Validate required fields 
    for (i=0;i<required.length;i++) { 
     var input = $('#'+required[i]); 
     if ((input.val() == "") || (input.val() == emptyerror)) { 
      input.addClass("needsfilled"); 
      input.val(emptyerror); 
      errornotice.fadeIn(750); 
     } else { 
      input.removeClass("needsfilled"); 
     } 
    } 


     // Only Letters. 
    if (!/^([a-zA-Z])+$/.test(errornotice.val())) { 
     errornotice.addClass("needsfilled"); 
     errornotice.val(onlyletters); 
    } 


    // Validate the e-mail. 
    if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) { 
     email.addClass("needsfilled"); 
     email.val(emailerror); 
    } 

    //if any inputs on the page have the class 'needsfilled' the form will not submit 
    if ($(":input").hasClass("needsfilled")) { 
     return false; 
    } else { 
     errornotice.hide(); 
     return true; 
    } 
}); 

// Clears any fields in the form when the user clicks on them 
$(":input").focus(function(){  
    if ($(this).hasClass("needsfilled")) { 
     $(this).val(""); 
     $(this).removeClass("needsfilled"); 
    } 
}); 
}); 
+2

只有信吗?您必须确定要确保O'Reilly先生无法注册。您正则表达式也仅仅局限于字母A-Z,让皮埃尔·将自己的运气了。 – Quentin

+1

该电子邮件正则表达式会说“不!”到很多完美的好电子邮件地址。 – Quentin

+0

提示的RFC822电子邮件正则表达式http://ex-parrot.com/~pdw/Mail-RFC822-Address.html – Jamiec

回答

1

如果这条线是针对测试或errornotice.val()firstname.val()

if (!/^([a-zA-Z])+$/.test(errornotice.val())) { 

// Maybe this is what you intended. 
// This requires adding some more variables earlier when you set email and errornotice 
email = $("#email"); 
errornotice = $("#error"); 
// Add vars for first/lastname 
firstname = $("#firstname"); 
lastname = $("#lastname"); 

if (!/^([a-zA-Z])+$/.test(firstname.val())) { 
    firstname.addClass("needsfilled"); 
    firstname.val(onlyletters); 
} 
// then do the same for lastname 
if (!/^([a-zA-Z])+$/.test(lastname.val())) { 
    lastname.addClass("needsfilled"); 
    lastname.val(onlyletters); 
} 

但是,您的信件正则表达式仅是要消除大量有效名称包括撇号,变音符号,变音符号的,等

+0

IM还挺新本所以任何帮助,将不胜感激 – Gezzamondo

+0

@ user874658你应该问自己,如果你真的想限制名称。我可能是“Michael O'Reilly Jones-Smith 1st”我个人不会对名称设置限制。 –

+0

如果我不验证表单,虽然即时通讯担心人们将能够通过PHP入侵我的数据库 – Gezzamondo