1)电话号码: (提示框换行 /r)
/^(/d{3,4}-)?/d{7,8}$/
2)电子邮件
/(/S)+[@]{1}(/S)+[.]{1}(/w)+/
匹配首尾空白字符的正则表达式:^/s*|/s*$
document.Form1.txtName.value.replace(/(^[/s]*)|([/s]*$)/g, "")
3)手机
/^13/d{9}$/.test( mobile ) | /^15/d{9}$/.test( mobile );
/d 代表数字
{n, m} 定义前面的描述出现n-m 次
? 前面描述出现 0 或 1 次
function
isPhone(s)
{
var patrn =/^ [ + ]{ 0 , 1 }(d){ 1 , 3 }[ ] ? ([ - ] ? ((d) | [ ]){ 1 , 12 }) + $ / ;
if ( ! patrn.exec(s)) return false
return true
}
function isPhone(value) {
var regex = /^ (d{ 3 , 4 } - ) ? d{ 7 , 8 }$ / ;
return regex.test(value);
}
{
var patrn =/^ [ + ]{ 0 , 1 }(d){ 1 , 3 }[ ] ? ([ - ] ? ((d) | [ ]){ 1 , 12 }) + $ / ;
if ( ! patrn.exec(s)) return false
return true
}
function isPhone(value) {
var regex = /^ (d{ 3 , 4 } - ) ? d{ 7 , 8 }$ / ;
return regex.test(value);
}
利用汉字Unicode范围来验证是否为汉字的JavaScript函数
function
checkchinese(str){
<
br
/>
var re1 = new RegExp( " ^[一-﨩]*$ " ) // 汉字的范围<br />
var re2 = new RegExp( " ^[-]*$ " ) < br />
var str = str.replace( / ( ^ s * ) | (s * $) / g, '' ); < br />
if (str == '' ){ return false ;} < br />
if ( ! (re1.test(str) && ( ! re2.test(str)))){ < br />
return false ; < br />
} < br />
return true ; < br />
}
var re1 = new RegExp( " ^[一-﨩]*$ " ) // 汉字的范围<br />
var re2 = new RegExp( " ^[-]*$ " ) < br />
var str = str.replace( / ( ^ s * ) | (s * $) / g, '' ); < br />
if (str == '' ){ return false ;} < br />
if ( ! (re1.test(str) && ( ! re2.test(str)))){ < br />
return false ; < br />
} < br />
return true ; < br />
}
*)用javascript函数来验证IP地址,电话号码,英文字母,数字2006-08-08 14:46
//
校验IP地址格式
// form3是表单名, ccc是文本框名
function checkip()
{
var scount = 0 ;
var ip = document.form3.ccc.value;
var iplength = ip.length;
var Letters = " 1234567890. " ;
for (i = 0 ; i < document.form3.ccc.value.length; i ++ )
{
var CheckChar = document.form3.ccc.value.charAt(i);
if (Letters.indexOf(CheckChar) == - 1 )
{
alert ( " IP地址格式不对!!,只能输入数字和“.”,格式为XXX.XXX.XXX.XXX 例如:192.168.0.1 " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
}
for ( var i = 0 ;i < iplength;i ++ )
(ip.substr(i, 1 ) == " . " ) ? scount ++ :scount;
if (scount != 3 )
{
alert ( " IP地址格式不对!,只能输入数字和“.”,格式为XXX.XXX.XXX.XXX 例如:192.168.0.1 " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
first = ip.indexOf( " . " );
last = ip.lastIndexOf( " . " );
str1 = ip.substring( 0 ,first);
subip = ip.substring( 0 ,last);
sublength = subip.length;
second = subip.lastIndexOf( " . " );
str2 = subip.substring(first + 1 ,second);
str3 = subip.substring(second + 1 ,sublength);
str4 = ip.substring(last + 1 ,iplength);
if (str1 == "" || str2 == "" || str3 == "" || str4 == "" )
{
alert( " 数字不能为空!格式为XXX.XXX.XXX.XXX 例如:192.168.0.1 " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
if (str1 < 0 || str1 > 255 )
{
alert ( " 数字范围为0~255! " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
else if (str2 < 0 || str2 > 255 )
{
alert ( " 数字范围为0~255! " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
else if (str3 < 0 || str3 > 255 )
{
alert ( " 数字范围为0~255! " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
else if (str4 < 0 || str4 > 255 )
{
alert ( " 数字范围为0~255! " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
}
// 校验是否为英文
function checkchar()
{
var Letters = " ABCDEFGHIJKLMNOPQRSTUVWXYZ "
for (i = 0 ; i < document.form.aaa.value.length; i ++ )
{
var CheckChar = document.form.aaa.value.charAt(i);
CheckChar = CheckChar.toUpperCase();
if (Letters.indexOf(CheckChar) == - 1 )
{
alert ( " 输入类型必须为英文! " );
return false ;
}
}
}
// 校验是否为数字
function checknum(p)
{
if (p == "" )
{alert ( " 输入不能为空! " );
return false ;
}
var l = p.length;
var count = 0 ;
for ( var i = 0 ; i < l; i ++ )
{
var digit = p.charAt(i);
if (digit == " . " )
{
++ count;
if (count > 1 )
{alert ( " 输入类型必须为数字! " );
return false ;
}
}
else if (digit < " 0 " || digit > " 9 " )
{alert ( " 输入类型必须为数字! " );
return false ;
}
}
return true ;
}
// 校验电话号码
function checkphone()
{
var Letters = " 1234567890, " ;
for (i = 0 ; i < document.form4.ddd.value.length; i ++ )
{
var CheckChar = document.form4.ddd.value.charAt(i);
if (Letters.indexOf(CheckChar) == - 1 )
{
alert ( " 电话号码只能为数字或逗号! " );
return false ;
}
}
}
</ script >
// form3是表单名, ccc是文本框名
function checkip()
{
var scount = 0 ;
var ip = document.form3.ccc.value;
var iplength = ip.length;
var Letters = " 1234567890. " ;
for (i = 0 ; i < document.form3.ccc.value.length; i ++ )
{
var CheckChar = document.form3.ccc.value.charAt(i);
if (Letters.indexOf(CheckChar) == - 1 )
{
alert ( " IP地址格式不对!!,只能输入数字和“.”,格式为XXX.XXX.XXX.XXX 例如:192.168.0.1 " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
}
for ( var i = 0 ;i < iplength;i ++ )
(ip.substr(i, 1 ) == " . " ) ? scount ++ :scount;
if (scount != 3 )
{
alert ( " IP地址格式不对!,只能输入数字和“.”,格式为XXX.XXX.XXX.XXX 例如:192.168.0.1 " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
first = ip.indexOf( " . " );
last = ip.lastIndexOf( " . " );
str1 = ip.substring( 0 ,first);
subip = ip.substring( 0 ,last);
sublength = subip.length;
second = subip.lastIndexOf( " . " );
str2 = subip.substring(first + 1 ,second);
str3 = subip.substring(second + 1 ,sublength);
str4 = ip.substring(last + 1 ,iplength);
if (str1 == "" || str2 == "" || str3 == "" || str4 == "" )
{
alert( " 数字不能为空!格式为XXX.XXX.XXX.XXX 例如:192.168.0.1 " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
if (str1 < 0 || str1 > 255 )
{
alert ( " 数字范围为0~255! " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
else if (str2 < 0 || str2 > 255 )
{
alert ( " 数字范围为0~255! " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
else if (str3 < 0 || str3 > 255 )
{
alert ( " 数字范围为0~255! " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
else if (str4 < 0 || str4 > 255 )
{
alert ( " 数字范围为0~255! " );
document.form3.ccc.value = "" ;
document.form3.ccc.focus();
return false ;
}
}
// 校验是否为英文
function checkchar()
{
var Letters = " ABCDEFGHIJKLMNOPQRSTUVWXYZ "
for (i = 0 ; i < document.form.aaa.value.length; i ++ )
{
var CheckChar = document.form.aaa.value.charAt(i);
CheckChar = CheckChar.toUpperCase();
if (Letters.indexOf(CheckChar) == - 1 )
{
alert ( " 输入类型必须为英文! " );
return false ;
}
}
}
// 校验是否为数字
function checknum(p)
{
if (p == "" )
{alert ( " 输入不能为空! " );
return false ;
}
var l = p.length;
var count = 0 ;
for ( var i = 0 ; i < l; i ++ )
{
var digit = p.charAt(i);
if (digit == " . " )
{
++ count;
if (count > 1 )
{alert ( " 输入类型必须为数字! " );
return false ;
}
}
else if (digit < " 0 " || digit > " 9 " )
{alert ( " 输入类型必须为数字! " );
return false ;
}
}
return true ;
}
// 校验电话号码
function checkphone()
{
var Letters = " 1234567890, " ;
for (i = 0 ; i < document.form4.ddd.value.length; i ++ )
{
var CheckChar = document.form4.ddd.value.charAt(i);
if (Letters.indexOf(CheckChar) == - 1 )
{
alert ( " 电话号码只能为数字或逗号! " );
return false ;
}
}
}
</ script >
5)去掉首尾空格:
function
trim(str)
{
return str.replace( /^ s * (. *? )[s ] * $ / g, ' $1 ' );
}
{
return str.replace( /^ s * (. *? )[s ] * $ / g, ' $1 ' );
}