zoukankan      html  css  js  c++  java
  • js验证函数

    ****************************************************

    功    能:验证函数集合

    说    明:

    版    本:1.0

    作    者:

    创建时间:2007-8-31

    ***************************************************

    */

    <!--

    //禁止输入空格

    function forbidSpace()

    {

        //onkeypress="forbidSpace();"

        if (window.event.keyCode==32)

        {

            alert('不允许输入空格!');

            window.event.keyCode = 0x0;

        }

    }

    //去除字符串空格

    function trim(s)

    {           

        return s.replace(/(^\s+)|(\s+$)/g,"");

    }

    //表单项是否为空

    function IsEmpty(pstr)

    {

        //alert(pstr)

        if (trim(pstr).length == 0)

        {

            return true;

        }

        return false;

    }

    //比较两个表单项的值是否相同

    function IsRepeat(obj1, obj2)

    {

        if ((obj1).value != (obj2).value)

        {

           return false;

        }

        return true;

    }

    //比较两个数值大小

    function CompairNum(value1,value2)

    {

        var ret;

        ret=eval(value1-value2);   

        if(ret>0 || isNaN(value1) || (value1.charAt(0)==0 && !isNaN(value1.charAt(1)) && value1.charAt(1)!='') )

        {

            alert('输入有误!');

            return false;

        }

        return true;

    }

    //比较两个数值大小

    function CompairNum2(value1,value2,note)

    {

        var ret;

        //alert(value1);

        ret=eval(value1-value2);   

        if(ret>0 || isNaN(value1) || (value1.charAt(0)==0 && !isNaN(value1.charAt(1)) && value1.charAt(1)!='') )

        {

            alert(note);

            return false;

        }

        return true;

    }

    //百分数[数字 . %]

    function percentage()

    {// 37 - %;

        if(!(window.event.keyCode>47&&window.event.keyCode<58||window.event.keyCode==46||window.event.keyCode==37))

        {

            window.event.keyCode=0x0;

        }

    }

    //身份证[数字 x]

    function identityCard()

    {

        if(!(window.event.keyCode>47&&window.event.keyCode<58||window.event.keyCode==120))

        {

            window.event.keyCode = 0x0;

            return false;

        }

        return true;

    }

    //格式必须仅为数字0-9

    function onlyNumeric()

    {// 0-9:48-57;-:45;

        //var outCha="40 41 45 59";

        if(window.event.keyCode<48||window.event.keyCode>57)

        {

            alert('只能输入数字!')

            window.event.keyCode = 0x0;

            return false;

        }

        return true;

    }

    //格式必须为数字和-

    function numericF()

    {

        if(!(window.event.keyCode>47&&window.event.keyCode<58||window.event.keyCode==45))

        {

            alert('只能输入数字!')

            window.event.keyCode=0x0;

        }

    }

    //格式必须为数字和.

    function numericD()

    {

        if(!(window.event.keyCode>47&&window.event.keyCode<58||window.event.keyCode==46))

        {

            alert('只能输入数字!')

            window.event.keyCode=0x0;

        }

    }

    //格式为数字,点(.)和负号(-)

    function numericDF()

    {

        if(!((window.event.keyCode>47&&window.event.keyCode<58)||window.event.keyCode==46||window.event.keyCode==45))

        {

            alert('只能输入数字!')

            window.event.keyCode=0x0;

            return (false);

        }

    }

    //格式必须为数字和,

    function numericB()

    {

        if(!(window.event.keyCode>47&&window.event.keyCode<58||window.event.keyCode==44))

        {

            alert('只能输入数字!')

            window.event.keyCode=0x0;

        }

    }

    //格式必须为数字.,

    function numericDB()

    {

        if(!(window.event.keyCode>47&&window.event.keyCode<58||window.event.keyCode==44||window.event.keyCode==46))

        {

            alert('只能输入数字!')

            window.event.keyCode=0x0;

        }

    }

    //格式必须为数字-,

    function numericFB()

    {

        if(!(window.event.keyCode>47&&window.event.keyCode<58||window.event.keyCode==44||window.event.keyCode==45))

        {

            alert('只能输入数字!')

            window.event.keyCode=0x0;

        }

    }

    //格式必须为数字.-,

    function numericDFB()

    {

        if(!(window.event.keyCode>47&&window.event.keyCode<58||window.event.keyCode==46||window.event.keyCode==44||window.event.keyCode==45))

        {

            alert('只能输入数字!')

            window.event.keyCode=0x0;

        }

    }

    //[127以内ASCII值] 非大小写26个英文字母

    function noLetter()

    {

        if ((window.event.keyCode>=65 && window.event.keyCode<=90)||(window.event.keyCode>=97&&window.event.keyCode<=122)||window.event.keyCode>127)

        {

            window.event.keyCode=0x0;

        }

    }

    //表单项只能为数字和"-",用于电话/银行帐号验证上,可扩展到域名注册等

    function IsNumberF(String)

    {

        var Letters = "1234567890-"; //可以自己增加可输入值

        var i;

        var c;

        if(String.charAt( 0 )=='-')

            return false;

        if( String.charAt( String.length - 1 ) == '-' )

            return false;

        for( i = 0; i < String.length; i ++ )

        {

            c = String.charAt( i );

            if (Letters.indexOf( c ) < 0)

            return false;

        }

        return true;

    }

    //数值检测,只为为0-9之间的数字

    function IsNumber(name)

    {

        if(name.length == 0)

            return false;

        for(i = 0; i < name.length; i++)

        {

            if(name.charAt(i) < "0" || name.charAt(i) > "9")

            return false;

        }

        return true;

    }

    //Digit evaluation function

    function isNum(passedVal)

    {

        if (passedVal == "")

            return false;

        else if (isNaN(passedVal))   

            return false;

           

        for (i=0; i<passedVal.length; i++)

        {

            if (passedVal.charAt(0) == "." || passedVal.charAt(passedVal.length-1) == "." || (passedVal.length > 1 && passedVal.charAt(0) == "0" && !isNaN(passedVal.charAt(1))))

                return false;

           

            else if (passedVal.charAt(0) == "-")

                i++;           

            else if (passedVal.charAt(i) < "0" && passedVal.charAt(i) != ".")

                return false;

            else if (passedVal.charAt(i) > "9" && passedVal.charAt(i) != ".")

                return false;

        }

        return true;

    }

    //↑↑↑//

    //判断是否为数值

    function checkNum(obj)

    {

        if (isNum(document.all(''+ obj +'').value)==false)

        {

            alert('非正确的数值输入!!!');

            document.all(''+ obj +'').focus();

            document.all(''+ obj +'').select();

            return false;

        }

        return true;

    }

    //表单项输入数值/长度限定

    function Limit(obj, min, max)

    {

        if ((obj).length > max || (obj).length < min)

        {

            return false;

        }

        return true;

    }

    //英文值检测

    function isEnglish(name)

    {

        if(name.length == 0)

            return false;

        for(i = 0; i < name.length; i++)

        {

            if(name.charCodeAt(i) > 128)

            return false;

        }

        return true;

    }

    //中文值检测

    function isChinese(name)

    {

        if(name.length == 0)

            return false;

        for(i = 0; i < name.length; i++)

        {

            if(name.charCodeAt(i) > 128)

            return true;

        }

        return false;

    }

    //限定表单项不能输入的字符

    function contain(str,charset)// 字符串包含测试函数

    {

      var i;

      for(i=0;i<charset.length;i++)

      if(str.indexOf(charset.charAt(i))>=0)

      return true;

      return false

    }

    资料引用:http://www.knowsky.com/442102.html

  • 相关阅读:
    Working with WordprocessingML documents (Open XML SDK)
    How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
    Azure:Manage anonymous read access to containers and blobs
    Convert HTML to PDF with New Plugin
    location.replace() keeps the history under control
    On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API
    HTTP Modules versus ASP.NET MVC Action Filters
    解读ASP.NET 5 & MVC6系列(6):Middleware详解
    Content Negotiation in ASP.NET Web API
    Action Results in Web API 2
  • 原文地址:https://www.cnblogs.com/nbalive2001/p/1383686.html
Copyright © 2011-2022 走看看