本文介绍了如何验证空文本框&amp;使用Java Script的HTML中的电子邮件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在HTML表单中,我需要验证不为空的文本框和一个电子邮件格式的电子邮件字段。 如果用户输入错误的电子邮件或空文本框,则应该发出警告信息。解决方案 试试这种方式: HTML: < form name = form1 method = action = onsubmit = return validateForm(this) > < 输入 type = text name = name value = 名称 / > < br / > < 输入 类型 = text name = addressLine01 value = 地址第1行 / > < br / > < input 类型 = 提交 / > < / form > JavaScript: function validateForm(form){ var nameField = form.name; var addressLine01 = form.addressLine01; if (isNotEmpty(nameField)){ if ( isNotEmpty(addressLine01)){ return true ; { { return false ; } function isNotEmpty(field){ var fieldData = field.value; if (fieldData.length == 0 || fieldData == || fieldData == fieldData){ field.className = FieldError; // 要突出显示错误的类 alert( 请更正错误以便继续。); return false ; } else { field.className = FieldOk; // 将字段重置为默认值 return true ; // 提交表单 } } 参考: http://www.htmlgoodies.com/beyond/article.php/3849281/Checking-Your-Work-验证输入---- Getting-Started.htm [ ^ ] http://mrbool.com/validation-in-javascript-emails-letters-and-empty-input-textbox/25472 [ ^ ] ..还可以参考 google [ ^ ] 使用html 5 1)发送电子邮件 E- mail:< input type =emailname =email> 2)用于验证文本框 用户名:< input type =textname =usrnamerequired> For Button(HTML) < input id =Submit1type =submitvalue =提交申请onclick =validateMyForm()/> 功能脚本: function validateMyForm() { var valid = true ; var validationMessage = ' 请更正以下错误:\\\\ n'; if (document.getElementById(' Text1')。 value .length == 0 ) { validationMessage = validationMessage + ' - 缺少名称\\\\ n'; 有效= false ; } 如果(文件.getElementById(' Text4')。 value .length == 0 ) { validationMessage = validationMessage + ' - 教育资格缺失\\\\ n'; 有效= false ; } 如果(文件.getElementById(' Text12')。 value .length!= 10 ) { validationMessage = validationMessage + ' - 手机号码不正确\\\\ n'; 有效= false ; } if (document.getElementById(' Text10')。 value .length = = 0 ) { validationMessage = validationMessage + ' - 电子邮件缺失\\\\ n'; 有效= false ; } 如果(有效== false ) { alert(validationMessage); } return 有效; } < / 脚本 > In HTML form I need to validate text boxes which is not to be empty and one e-mail field which is to be in e-mail format. If user enters wrong e-mail or empty text box, it should throw a alert msg. 解决方案 Try this way:HTML: <form name="form1" method="" action="" onsubmit="return validateForm(this)"> <input type="text" name="name" value="Name"/><br /> <input type="text" name="addressLine01" value="Address Line 1"/><br /> <input type="submit"/> </form>JavaScript: function validateForm(form) { var nameField = form.name; var addressLine01 = form.addressLine01; if (isNotEmpty(nameField)) { if(isNotEmpty(addressLine01)) { return true; { { return false; } function isNotEmpty(field) { var fieldData = field.value; if (fieldData.length == 0 || fieldData == "" || fieldData == fieldData) { field.className = "FieldError"; //Classs to highlight error alert("Please correct the errors in order to continue."); return false; } else { field.className = "FieldOk"; //Resets field back to default return true; //Submits form } }Refer:http://www.htmlgoodies.com/beyond/article.php/3849281/Checking-Your-Work-Validating-Input----Getting-Started.htm[^]http://mrbool.com/validation-in-javascript-emails-letters-and-empty-input-textbox/25472[^]..and also refer google[^]use html 5 1)for emailE-mail: <input type="email" name="email">2)for validating textboxUsername: <input type="text" name="usrname" required>For Button(HTML)<input id="Submit1" type="submit" value="Submit Application" onclick="validateMyForm()"/> Script for Function:function validateMyForm() { var valid = true; var validationMessage = 'Please correct the following errors:\r\n'; if (document.getElementById('Text1').value.length == 0) { validationMessage = validationMessage + ' - Name is missing\r\n'; valid = false; } if (document.getElementById('Text4').value.length == 0) { validationMessage = validationMessage + ' - Educational Qualification is missing\r\n'; valid = false; } if (document.getElementById('Text12').value.length != 10) { validationMessage = validationMessage + ' - Cellphone Number is not correct\r\n'; valid = false; } if (document.getElementById('Text10').value.length == 0) { validationMessage = validationMessage + ' - Email is missing\r\n'; valid = false; } if (valid == false) { alert(validationMessage); } return valid; }</script> 这篇关于如何验证空文本框&amp;使用Java Script的HTML中的电子邮件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 00:34