123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- var validate_flag=false;
- function my_validate(){
- if(mui.os.ios){
- mui.each(document.querySelectorAll('.my-validate'), function(index, el) {
- el.onblur=function(){
- validate_bind(el);
- }
- });
- }else{
- mui.each(document.querySelectorAll('.my-validate'), function(index, el) {
- el.onkeydown=function(){
- var keyNum=window.event ? e.keyCode :e.which; //获取被按下的键值
- mui.alert("键值:"+keyNum)
- if(keyNum==13){
- validate_bind(el);
- }
- }
- });
- }
- }
- function validate_bind(obj){
- var tel =/^(0?(13|14|15|18)[\d]{9}$|(01|02|03|04|05|06|07|08|09)[\d]{7,10}$|(400|800)[0-9]{7}$)/; //手机,座机(400-800)
- var mobile=/^0?(13|14|15|16|18|19)[0-9]{9}$/; //手机号码
- var ch=/^[\u4e00-\u9fa5]{1,}$/;//简体中文
- var ch_en_num=/^[A-Za-z0-9_\-\u4e00-\u9fa5]{1,}$/;//简体中文英文数字
- var email=/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/; //邮箱
- var http=/^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+$/; //网址
- var doublebyte=/^[^\x00-\xff]+$/; //简体+繁体中文(包括中文全角符号,全角空格)
- var positive_int=/^[0-9]\d*$/; //正整数0-9
- var negtive_int=/^-[1-9]\d*$/; //负整数
- var qq=/^[1-9]([0-9]{4,12})$/; //腾讯QQ号
- var postalcode=/^\d{6}$/; //中国邮政编码
- var ip=/^(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)$/;//ip地址
- var card_id=/^(\d{17}[\d|x]|\d{15})$/; //身份证(国内)
- var full_date=/^\d{4}(\-|\/|.)\d{1,2}\1\d{1,2}$/; //日期格式 (2016\12\12,2016-12-12,2016.12.12)
-
- switch (obj.getAttribute('validate-type')){
- case 'tel':
- if(tel.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- obj.focus();
- focus_end(obj)
- mui.alert('请输入正确的电话或手机号');
- return false;
- }
- break;
- case 'mobile':
- if(mobile.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- obj.focus();
- focus_end(obj)
- mui.alert('请输入正确的手机号');
- return false;
- }
- break;
- case 'ch':
- if(ch.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入简体中文');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'ch_en_num':
- if(ch_en_num.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入中、英文或数字');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'email':
- if(email.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入正确的邮箱地址');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'http':
- if(http.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入正确的网址');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'doublebyte':
- if(doublebyte.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入简体、繁体中文');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'positive_int':
- if(positive_int.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入正整数');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'negtive_int':
- if(negtive_int.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入负整数');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'qq':
- if(qq.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入正确的QQ号');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'postalcode':
- if(postalcode.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入正确的邮政编码');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'ip':
- if(ip.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入正确的IP地址');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'card_id':
- if(card_id.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入正确的身份证号');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- case 'full_date':
- if(full_date.test(obj.value)){
- validate_flag=true;
- }else{
- validate_flag=false;
- mui.alert('请输入正确的日期格式');
- obj.focus();
- focus_end(obj)
- return false;
- }
- break;
- default:
- break;
- }
- }
- function focus_end(obj){
- var len = obj.value.length;
- if (document.selection) {
- var sel = obj.createTextRange();
- sel.moveStart('character', len);
- el.collapse();
- sel.select();
- } else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
- obj.selectionStart = obj.selectionEnd = len;
- }
- }
|