现在的位置: 首页 -> 前端学习 -> JS前端 -> js&jquery验证邮箱和手机号是否正确范例

js&jquery验证邮箱和手机号是否正确范例

2014-01-13 22:14评论数 0 ⁄ 被浏览 9365 views+

js验证邮箱或者手机号是网页制作中经常用到的,特此分享一份js验证邮箱和手机号的函数代码,收藏一下,受用终身。用jquery写的,因此不要忘了调用jquery库。


演示地址:https://www.daixiaorui.com/Public/demo/js/check_email_mobile.html 


实现源码:


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript" src="https://www.daixiaorui.com/Public/js/jquery.min.js"></script>

<script type="text/javascript">

$(function(){

$("#ck_mobile").click(function(){

var mobile = $("#mobile").val();

if(!check_mobile(mobile)){

alert("手机号码格式不正确");

}else{

alert("手机号码正确");

}

});

$("#ck_email").click(function(){

var email = $("#email").val();

if(!check_email(email)){

alert("email格式不正确");

}else{

alert("email正确");

}

});

})


//检查手机号码

function check_mobile(mobile){

if(mobile.length != 11 || isNaN(mobile)){

return false;

}

mobile = mobile.substr(0,3);

//号段

var hd = new Array('130','131','132','133','134','135','136','137','138','139','150','151','152','153','154','155','156','157','158','159','180','181','182','183','184','185','186','187','188','189');

var i = hd.length;

while (i--) {

if (hd[i] == mobile) {

return true;

}   

}   

return false;

}


//验证邮箱是否正确

function check_email(email){

var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;

if(!reg.test(email)){

return false;

}else{

return true;

}

}

</script>


<p>手机号:<input type="text" id="mobile"  /> <input type="button" value="验证手机号" id="ck_mobile" /></p>

<p>Email:<input type="text" id="email"  /> <input type="button" value="验证邮箱格式" id="ck_email" /></p>

 

文章出自:https://www.daixiaorui.com/read/68.html 本站所有文章,除注明出处外皆为原创,转载请注明本文地址,版权所有。

目前有 0 条评论  @我要评论

    您的每一个评论都是对我的一份支持

     博客二维码

    昵称 *

    邮箱 *