JS 校验
2007-03-06 15:44
155 查看
JavaScript can be used to validate input data in HTML forms before sending off the content to a server.
JS可以用来校验HTML表单提交给服务器的数据(是否符合规范)
JavaScript Form Validation
JavaScript can be used to validate input data in HTML forms before sending off the content to a server.
JS可以用来校验HTML表单提交给服务器的数据(老外就是喜欢重复)
Form data that typically are checked by a JavaScript could be:
典型的JS检查表单数据有以下:
has the user left required fields empty?
用户是否没有填写必须添加的内容?
has the user entered a valid e-mail address?
用户是否填写了有效的EMAIL地址?
has the user entered a valid date?
用户是否写了有效的日期?
has the user entered text in a numeric field?
用户是否在应该填写数字的地方写了文字?
Required Fields
The function below checks if a required field has been left empty. If the required field is blank, an alert box alerts a message and the function returns false. If a value is entered, the function returns true (means that data is OK):
下面的函数检查了必添的内容有没空掉。如果空了的话就会出现警告信息并返回false。如果写了内容那就返回true(意思就是数据没问题)
The entire script, with the HTML form could look something like this:
完整的脚本合起HTML表单:
E-mail Validation
The function below checks if the content has the general syntax of an email.
下面的函数会检查email内容的语法。
This means that the input data must contain at least an @ sign and a dot (.). Also, the @ must not be the first character of the email address, and the last dot must at least be one character after the @ sign:
意思就是输入的内容必须有起码一个@标记和一个点(.)而且@标记不能是email地址的第有个符号,点必须是在@标记后出现:
The entire script, with the HTML form could look something like this:
完整的样子是这样(加入了HTML表单):
JS可以用来校验HTML表单提交给服务器的数据(是否符合规范)
JavaScript Form Validation
JS表单校验
JavaScript can be used to validate input data in HTML forms before sending off the content to a server.JS可以用来校验HTML表单提交给服务器的数据(老外就是喜欢重复)
Form data that typically are checked by a JavaScript could be:
典型的JS检查表单数据有以下:
has the user left required fields empty?
用户是否没有填写必须添加的内容?
has the user entered a valid e-mail address?
用户是否填写了有效的EMAIL地址?
has the user entered a valid date?
用户是否写了有效的日期?
has the user entered text in a numeric field?
用户是否在应该填写数字的地方写了文字?
Required Fields
必填的内容
The function below checks if a required field has been left empty. If the required field is blank, an alert box alerts a message and the function returns false. If a value is entered, the function returns true (means that data is OK):下面的函数检查了必添的内容有没空掉。如果空了的话就会出现警告信息并返回false。如果写了内容那就返回true(意思就是数据没问题)
function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") {alert(alerttxt);return false} else {return true} } } |
完整的脚本合起HTML表单:
<html> function validate_form(thisform) { with (thisform) { if (validate_required(email,"Email must be filled out!")==false) {email.focus();return false} } } </script> </head> <body> <form action="submitpage.htm" onsubmit="return validate_form(this)" method="post"> Email: <input type="text" name="email" size="30"> <input type="submit" value="Submit"> </form> </body> </html> |
E-mail Validation
E-mail校验
The function below checks if the content has the general syntax of an email.下面的函数会检查email内容的语法。
This means that the input data must contain at least an @ sign and a dot (.). Also, the @ must not be the first character of the email address, and the last dot must at least be one character after the @ sign:
意思就是输入的内容必须有起码一个@标记和一个点(.)而且@标记不能是email地址的第有个符号,点必须是在@标记后出现:
function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@") dotpos=value.lastIndexOf(".") if (apos<1||dotpos-apos<2) {alert(alerttxt);return false} else {return true} } } |
完整的样子是这样(加入了HTML表单):
<html> function validate_form(thisform) { with (thisform) { if (validate_email(email,"Not a valid e-mail address!")==false) {email.focus();return false} } } </script> </head> <body> <form action="submitpage.htm" onsubmit="return validate_form(this);" method="post"> Email: <input type="text" name="email" size="30"> <input type="submit" value="Submit"> </form> </body> </html> |
相关文章推荐
- js中url和ip地址校验
- js校验常用方法
- Servlet+js实现简单验证码校验
- 常见的js校验
- js区分汉字和字符,校验长度
- 前台jsp校验:js+Ajax+正则表达式--【DRP】
- js校验表单后提交表单的三种方法总结
- 使用jquery-validationEngine框架,4步实现前端JS校验
- react.js-06-propTypes类型校验
- js正则表达式校验非负整数:^\d+$ 或 ^[1-9]\d*|0$
- Atom 编辑器安装 linter-eslint 插件,并配置使其支持 vue 文件中的 js 格式校验
- 2014-08-19 09:43:10 js校验
- eclipse(myeclipse、sts)保存js文件校验缓慢的解决办法
- Jquery Easyui验证扩展,Easyui验证,Easyui校验,js正则表达式
- 使用struts 怎么才能在执行完js校验页面的信息,发现不全的情况下,不让页面提交到Action中
- js中常用表单数据校验方法
- 彻底关掉MyEclipse的自动校验,特别是对js文件的校验!!
- js计算字符串长度、汉字校验、去除空格、数字校验、身份证校验的方法
- js 正则中文校验
- js(严格)校验身份证号_easyCheckIdcard