最简单的JavaScript validation

本文介绍了一种使用JavaScript进行表单验证的方法,包括检查整数、浮点数、字符串最大长度及日期格式等。通过具体函数实现不同类型的输入校验,并提供了一个HTML测试示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JS validation、JS check、Form check

    //obj - input object
    //required 是否必填
    //type 校验类型,date-日期类型,num-浮点数字类型,lnum=整数
    //maxLen - 长度校验
     
      //校验整数
      function checkLNum(theNum){
              var patrn=/^[0-9]{1,20}$/;
              if (!patrn.exec(theNum)) return false
              return true  
      }
     
      //校验浮点数
      function checkNum(theNum){
              var patrn=/^[0-9.]{1,20}$/;
              if (!patrn.exec(theNum)) return false
              return true  
      }
     
      //校验最大长度
      function checkMaxLen(theStr,maxLen){
            if(theStr.length > maxLen)
                  return false;
            return true;
      }
     
      //校验日期格式,YYYY- MM-DD
      function checkDate(theDate){
        var reg = /^\d{4}-((0{0,1}[1-9]{1})|(1[0-2]{1}))-((0{0,1}[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1}))$/;
        var result=true;
        if(!reg.test(theDate))
            result = false;
        else{
            var arr_hd=theDate.split("-");
            var dateTmp;
            dateTmp= new Date(arr_hd[0],parseFloat(arr_hd[1])-1,parseFloat(arr_hd[2]));
            if(dateTmp.getFullYear()!=parseFloat(arr_hd[0])
                  || dateTmp.getMonth()!=parseFloat(arr_hd[1]) -1
                    || dateTmp.getDate()!=parseFloat(arr_hd[2])){
                    result = false
            }
        }
        return result;
      }

  function getSpanObject(spanId){
        return document.getElementById(spanId);
  }
 

HTML测试代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title>JS Check</title>
<script Language=JavaScript src="check.js"></script>
<script Language=JavaScript>
            function formSubmit(){
                  var flag = check(testForm.startDate,true,'date',12);
                  flag = check(testForm.total,false,'num') && flag;
                  flag = check(testForm.name,true,'other',6) && flag;
                  if(flag){
                        return testForm.submit();
                  }
            }
</script>
</head>
<body>
      <form name="testForm">
            Date:<input type="text" name="startDate" onblur="check(this,true,'date',12)" /><span id="startDateErr" style="color:red"></span><br/>
            Num:<input type="text" name="total" onblur="check(this,false,'num')"/><span id="totalErr" style="color:red"></span><br/>
            String:<input type="text" name="name" onblur="check(this,true,'other',6)"/><span id="nameErr" style="color:red"></span><br/>
            <input type="button" value="Submit" onclick="formSubmit()"/>
      <form>
</body>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值