這裡給大家提供幾個有用的Javascript驗證腳本以下是代碼片段供大家參考
function isNum(numlowhi) {
if(isNaN(num)||num<low||num>hi)return false;
return true;
}
function isValidIP(v) {
nums=vsplit();
if(numslength!=)return false;
for(j=;j<;j++){
if(!isNum(nums[j]))return false;
}
return true;
}
function checkIP(ObjectDescMaybeEmpty)
{
var strValue= Objectvalue;
if(MaybeEmpty){
if(strValuelength ==){
return true;
}
}
if(isValidIP(strValue)==false)
{
alert(Desc + Format Error!);
Objectfocus();
return false;
}
return true;
}
function checkPort(ObjectDesclowestMaybeEmpty)
{
var pattern=/^\d{}$/;
var strValue= Objectvalue;
if(MaybeEmpty){
if(strValuelength ==){
return true;
}
}
if(strValuematch(pattern) == null)
{
alert(Desc + Format Error!);
Objectfocus();
return false;
}
if (strValue<lowest || strValue >)
{
alert(Desc + Format Error!);
Objectfocus();
return false;
}
return true;
}
function checkFitLongStr(ObjectDescMaybeEmpty)
{
var pattern=/^\S{}$/;
var strValue= Objectvalue;
if(MaybeEmpty){
if(strValuelength ==){
return true;
}
}
if(strValuematch(pattern) == null)
{
alert(Desc + Format Error!);
Objectfocus();
return false;
}
return true;
}
function checkFitLongNum(ObjectDescMaybeEmpty)
{
var pattern=/^\d{}$/;
var strValue= Objectvalue;
if(MaybeEmpty){
if(strValuelength ==){
return true;
}
}
if(strValuematch(pattern) == null)
{
alert(Desc + Format Error!);
Objectfocus();
return false;
}
return true;
}
function checkGC(ObjectDesc)
{
var pattern = /^\{}\d{}$/
var strValue = Objectvalue;
if (strValuelength ==){
return true;
}
if(strValuematch(pattern) == null)
{
alert(Desc + Format Error!);
Objectfocus();
return false;
}
if (strValue < ||strValue > )
{
alert(Desc + Format Error!);
Objectfocus();
return false;
}
return true;
}
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19702.html