1.验证输入是否为手机号码或电话号码
代码
1 <script type="text/javascript">
2 String.prototype.Trim = function() {
3 var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);
4 return (m == null) ? "" : m[1];
5 }
6
7 String.prototype.isMobile = function() {
8 return (/^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})$/.test(this.Trim()));
9 }
10
11 String.prototype.isTel = function()
12 {
13 //"兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"
14 //return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
15 return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
16 }
17
18 function chkForm() {
19 with(document.form1){
20 if (tel.value.isMobile()||tel.value.isTel()) {
21 tel.value = tel.value.Trim();
22 alert("您的电话/手机号码是:" + tel.value);
23 return true;
24 }
25 else {
26 alert("请输入正确的手机号码或电话号码\n\n例如:13916752109或0712-3614072");
27 tel.focus();
28 return false;
29 }
30 }
31 }
32 </script>
33 <form name="form1" method="post" action="" >
34 <input type="text" name="tel" value="13916752109" size="15" />
35 <input type="button" value="测 试" onclick="return chkForm()"/>
36 </form>
37
2 String.prototype.Trim = function() {
3 var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);
4 return (m == null) ? "" : m[1];
5 }
6
7 String.prototype.isMobile = function() {
8 return (/^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})$/.test(this.Trim()));
9 }
10
11 String.prototype.isTel = function()
12 {
13 //"兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"
14 //return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
15 return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
16 }
17
18 function chkForm() {
19 with(document.form1){
20 if (tel.value.isMobile()||tel.value.isTel()) {
21 tel.value = tel.value.Trim();
22 alert("您的电话/手机号码是:" + tel.value);
23 return true;
24 }
25 else {
26 alert("请输入正确的手机号码或电话号码\n\n例如:13916752109或0712-3614072");
27 tel.focus();
28 return false;
29 }
30 }
31 }
32 </script>
33 <form name="form1" method="post" action="" >
34 <input type="text" name="tel" value="13916752109" size="15" />
35 <input type="button" value="测 试" onclick="return chkForm()"/>
36 </form>
37
2.验证只能输入数字
代码
<SCRIPT LANGUAGE="JavaScript">
<!--
function limitNum(obj) {
if(obj.value.replace(/[\d+]/ig,"").length>0) {
alert('请输入数字')
}
}
//-->
</SCRIPT>
<input name="textfield1" type="text" id="textfield1" style="width:60;" onKeyup="limitNum(this)" />
<!--<asp:textbox name="dealtime" type="text" size="25" id="dealtime" runat="server" Width="42px" MaxLength="5" onpropertychange="limitNum(this)"></asp:textbox>-->
<!--
function limitNum(obj) {
if(obj.value.replace(/[\d+]/ig,"").length>0) {
alert('请输入数字')
}
}
//-->
</SCRIPT>
<input name="textfield1" type="text" id="textfield1" style="width:60;" onKeyup="limitNum(this)" />
<!--<asp:textbox name="dealtime" type="text" size="25" id="dealtime" runat="server" Width="42px" MaxLength="5" onpropertychange="limitNum(this)"></asp:textbox>-->