How to validate email field in javascript?
这是我用于表单验证的脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <script language="JavaScript"> function formCheck(formobj){ var fieldRequired = Array("Name","Email","Phone","comments"); var fieldDescription = Array("Name","Email","Phone","Comments"); var alertMsg ="Please complete the following fields: "; var l_Msg = alertMsg.length; for (var i = 0; i < fieldRequired.length; i++){ var obj = formobj.elements[fieldRequired[i]]; if (obj){ switch(obj.type){ case"select-one": if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text ==""){ alertMsg +=" -" + fieldDescription[i] +" "; } break; case"select-multiple": if (obj.selectedIndex == -1){ alertMsg +=" -" + fieldDescription[i] +" "; } break; case"text": case"textarea": if (obj.value =="" || obj.value == null){ alertMsg +=" -" + fieldDescription[i] +" "; } break; default: } if (obj.type == undefined){ var blnchecked = false; for (var j = 0; j < obj.length; j++){ if (obj[j].checked){ blnchecked = true; } } if (!blnchecked){ alertMsg +=" -" + fieldDescription[i] +" "; } } } } if (alertMsg.length == l_Msg){ return true; }else{ alert(alertMsg); return false; } } // --> <form onsubmit="return formCheck(this);" action="/capture.weblead" method="post"> First Name: <input type=text name="FirstName" size="25"> Last Name: <input type=text name="LastName" size="25"> <input type=submit value="Submit Form"> </form> |
除了不能验证真实的电子邮件地址外,它工作得很好。如何更改此表单以使其生效?
脚本不能包含任何美元符号,否则Tomcat(我的服务器环境)崩溃。
您可以在javascript中使用regex
1 2 3 | function is_email(email){ var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; return emailReg.test(email); } |
号
http://richwd.com/email-javascript
我建议您使用jquery和验证插件:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/验证/
它灵活可靠,使用方便。
这是一个快速而肮脏的解决方案。根据需要修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | function formCheck(formobj){ var fieldRequired = Array("Name","Email","Phone","comments"); var fieldDescription = Array("Name","Email","Phone","Comments"); var alertMsg ="Please complete the following fields: "; var emailRegexp =/@/; var l_Msg = alertMsg.length; for (var i = 0; i < fieldRequired.length; i++){ var obj = formobj.elements[fieldRequired[i]]; if (obj){ switch(obj.type){ case"select-one": if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text ==""){ alertMsg +=" -" + fieldDescription[i] +" "; } break; case"select-multiple": if (obj.selectedIndex == -1){ alertMsg +=" -" + fieldDescription[i] +" "; } break; case"text": case"textarea": if ( obj.value =="" || obj.value == null || ( fieldRequired[i] =="Email" && !obj.value.match(emailRegexp) )) { alertMsg +=" -" + fieldDescription[i] +" "; } break; default: } if (obj.type == undefined){ var blnchecked = false; for (var j = 0; j < obj.length; j++){ if (obj[j].checked){ blnchecked = true; } } if (!blnchecked){ alertMsg +=" -" + fieldDescription[i] +" "; } } } } if (alertMsg.length == l_Msg){ return true; }else{ alert(alertMsg); return false; } } |