JS 校验

 

JavaScript can be used to validate input data in HTML forms before sending off the content to a server.
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}
}
}

The entire script, with the HTML form could look something like this:
完整的脚本合起HTML表单:

<html>
<head>
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false}
else {return true}
}
}
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"
οnsubmit="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}
}
}

The entire script, with the HTML form could look something like this:
完整的样子是这样(加入了HTML表单):

<html>
<head>
<script type="text/javascript">
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}
}
}
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"
οnsubmit="return validate_form(this);"
method="post">
Email: <input type="text" name="email" size="30">

<input type="submit" value="Submit"> 
</form>
</body>
</html>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值