在客户端JAVASCRITP处理TEXTFORM的函数

发表于:2007-06-30来源:作者:点击数: 标签:
HTML HEAD META NAME=GENERATOR Content=Microsoft Visual Studio 6.0 TITLE/TITLE /HEAD BODY FORM action= method=post id=form1 name=form1 onsubmit=return CheckForm() PINPUT id=text1 name=text1INPUT id=submit1 name=submit1 type=submit value=Subm
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>
<FORM action="" method=post id=form1 name=form1 onsubmit="return CheckForm()">

<P><INPUT id=text1 name=text1><INPUT id=submit1 name=submit1 type=submit value=Submit></P></FORM>
<P> </P>
<P> </P>
<P> </P>
<SCRIPT LANGUAGE=javascript>
<!--
function KillSpace(x){
        while((x.length>0) && (x.charAt(0)==@# @#))
                x = x.substring(1,x.length);
        while((x.length>0) && (x.charAt(x.length-1)==@# @#))
                x = x.substring(0,x.length-1);
        return x;
}

function CheckText(Obj,TypeValue,Msg,Necessary)
{
    /*TypeValue
    @#d@#: Numeric;
    @#i@#: All Integer
    @#r@#: Aeal Number
    @#f@#: Decimal Fraction
    @#l@#: 26 letter
    @#ul@#:[A-Z]
    @#ll@#:[a-z]
    @#w@#: [a-zA-Z_0-9]
    @#S@#: Chinese Characters and Special Characters
    
    Obj Required.
    Msg Return Prompt Message
    Necessary is Optional Parameter
    */
    if (isNaN(Necessary))
        Necessary=false;
    Obj.value=KillSpace(Obj.value);
    switch (TypeValue)
    {
        case "d":
            res=new RegExp("^[0-9]+$","g");
            break;
        case "i":
            res=new RegExp("(^[1-9][0-9]*$)|(^[\+\-][1-9][0-9]*$)","g");
            break
        case "r":
            res=new RegExp("(^([\+\-]?[1-9])[0-9]*(\.[0-9]+)?[0-9]$)|(^([\+\-]?[0-9]\.)[0-9]+$)","g")
            break;
        case "f":
            res=new RegExp("(^(0\.)[0-9]+$)|(^[\+\-]0\.[0-9]+$)","g");
            break;
        case "l":
            res=new RegExp("^[a-z]+$","gi");
            break;
        case "ul":
            res=new RegExp("^[A-Z]+$","g");
            break;
        case "ll":
            res=new RegExp("^[a-z]+$","g");
            break;
        case "w":
            res=new RegExp("^[0-9a-zA-Z_]+$","g")
            break;
        case "S":
            res=new RegExp("[^a-zA-Z0-9\n\r\t\f\v~`!@#$%^&*()\_\+\\/\.,<>\? ]+$","g")
            break;
        default:
            res=new RegExp("\s+","g");
    }
    //Main Process    
    IsOk=res.exec(Obj.value);
    if ((Necessary && !IsOk)||(!IsOk && Obj.value.length > 0 ))
        {
            alert(Msg);
            Obj.focus();
            Obj.select();
            return false;
        }
    else
        return true;

}
function CheckForm()
{
    CheckText(document.form1.text1,"S","请正确的格式!",true);
    return false;
        
}
//-->
</SCRIPT>

</BODY>
</HTML>
注:如果需要强制只能输入中文,需要另行编写代码。

以上是本人重写了以前的函数,还没有应用,如果发现其中有错误请告诉本人
email:peace.season@sohu.com

原文转自:http://www.ltesting.net