Home Web Front-end JS Tutorial JS函数验证总结(方便js客户端输入验证)_javascript技巧

JS函数验证总结(方便js客户端输入验证)_javascript技巧

May 16, 2016 pm 06:17 PM

复制代码 代码如下:



JS函数验证总结
复制代码 代码如下:

//去除左侧空格
function LTrim(str)
{
return str.replace(/^\s*/g,"");
}

//去右空格
function RTrim(str)
{
return str.replace(/\s*$/g,"");
}

//去掉字符串两端的空格
function trim(str)
{
return str.replace(/(^\s*)|(\s*$)/g, "");
}

//去除字符串中间空格
function CTim(str)
{
return str.replace(/\s/g,'');
}

//是否为由数字组成的字符串
function is_digitals(str)
{
var reg=/^[0-9]*$/;//匹配整数
return reg.test(str);
}

//验证是否为整数,包括正负数;
function Is_Int(str)
{
var reg=/^(-|\+)?\d+$/;
return reg.test(str);
}

//是大于0的整数
function Is_positive_num(str)
{
var reg=/^\d+$/;
return reg.test(str);
}

//负整数的验证
function Is_minus(str)
{
var reg=/^-\d+$/;
return reg.test(str);
}

//验证是否为浮点数(正数)
function IsPositiveFloat(str)
{
var check_float =new RegExp("^[1-9][0-9]*\.[0-9]+$");//匹配浮点数
return check_float.exec(str);
}

//是否为固定电话,区号3到4位,号码7到8位,区号和号码用"-"分割开,转接号码为1到6位,用小括号括起来紧跟在号码后面
function IsTelphone(str)
{
var reg=/^[0-9]{3,4}\-\d{7,8}(\(\d{1,6}\))?$/;

if (reg.test(str))
return true;
else
return false;
}

//手机号码验证,验证13系列和158,159几种号码,长度11位
function IsMobel(str)
{
var reg0 = /^13\d{9}$/;
var reg1 = /^158\d{8}$/;
var reg2 = /^159\d{8}$/;

return (reg0.test(str)||reg1.test(str)||reg2.test(str))
}

//验证是否为中文
function IsChinese(str)
{
var reg=/^[\u0391-\uFFE5]+$/;
return reg.test(str);
}

//验证是否为qq号码,长度为5-10位
function IsQq(str)
{
var reg=/^[1-9]\d{4,9}$/;
return reg.test(str);
}

//验证邮编
function IsPostId(str)
{
var reg=/^\d{6}$/;
return reg.test(str);
}

//验证是否未email
function IsEmail(str)
{
var reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
return reg.test(str);
}

//验证IP地址
function IsIp(str)
{
var check=function(v)
{
try
{
return (v=0)
}catch(x){
return false;
}
}
var re=str.split(".")
return (re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false
}

//身份证验证
function IsIdnum(str)
{
var City={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",
31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",
43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",
61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "}
var iSum=0
var info=""
if(!/^\d{17}(\d|x)$/i.test(str))
return false;
str=str.replace(/x$/i,"a");
if(City[parseInt(str.substr(0,2))]==null)
{
alert( "Error:非法地区");
return false;
}
sBirthday=str.substr(6,4)+"-"+Number(str.substr(10,2))+"-"+Number(str.substr(12,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
{
alert("Error:非法生日");
return false;
}
for(var i = 17;i>=0;i --)
iSum += (Math.pow(2,i) % 11) * parseInt(str.charAt(17 - i),11)
if(iSum%11!=1)
{
alert("Error:非法证号");
return false;
}
return City[parseInt(str.substr(0,2))]+","+sBirthday+","+(str.substr(16,1)%2?"男":"女")
}

//判断是否短时间,形如 (13:04:06)
function IsTime(str)
{
var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
if (a == null)
{
alert('输入的参数不是时间格式'); return false;
}
if (a[1]>24 || a[3]>60 || a[4]>60)
{
alert("时间格式不对");
return false
}
return true;
}

//短日期,形如 (2003-12-05)
function IsDate(str)
{
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)
return false;
var d= new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}

// 长时间,形如 (2003-12-05 13:04:06)
function IsDateTime(str)
{
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if(r==null)
return false;
var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
}

// 判断字符全部由a-Z或者是A-Z的字字母组成
function Is_Letters(str)
{
var reg=/[^a-zA-Z]/g;
return reg.test(str);
}

// 判断字符由字母和数字组成。
function Is_letter_num(str)
{
var reg=/[^0-9a-zA-Z]/g;
return reg.test(str);
}

//判断字符由字母和数字,下划线,点号组成.且开头的只能是下划线和字母
function IsUserName(str)
{
var reg=/^([a-zA-z_]{1})([\w]*)$/g;
return reg.test(str);
}

// 判断浏览器的类型
function GetBrowseType()
{
alert(window.navigator.appName);
}

//判断ie的版本
function Get_Eidition()
{
alert(window.navigator.appVersion);
}

//判断客户端的分辨率
function GetResolution()
{
alert(window.screen.height);
alert(window.screen.width);
}

// 判断用户名是否为数字字母下滑线
function notchinese(str)
{
var reg=/[^A-Za-z0-9_]/g
if (reg.test(str))
{
return (false);
}
else
{
return(true);
}
}

//验证url
function IsUrl(str)
{
var reg=/^(http\:\/\/)?([a-z0-9][a-z0-9\-]+\.)?[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+(\/[a-z0-9\.\,\-\_\%\?\=\&]?)?$/i;
return reg.test(str);
}

//判断是否含有汉字
function ContentWord(str)
{
if (escape(str).indexOf("%u")!=-1)
return true;
else
return false;
}

//页面里回车到下一控件的焦点
function Enter2Tab(e)
{
try
{
var ōb = IsFireFox ? e.target : event.srcElement;
if(ob.tagName == "INPUT" &&(ob.type == "text" ||ob.type == "password" ||ob.type == "checkbox"
||ob.type == "radio") ||ob.tagName == "SELECT")
{
var key = IsFireFox ? e.which : event.keyCode;
if (key == 13)
{
if (IsFireFox)
{
event.which = 9;
}
else
{
event.keyCode = 9;
}
}
}
}
catch(E){}
}

/**
* 初始化一个xmlhttp对象
*/
function InitAjax()
{
var ajax=false;
  try
{
   ajax = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e)
{
   try
{
    ajax = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E)
{
    ajax = false;
   }
  }
  if (!ajax && typeof XMLHttpRequest!='undefined')
{
   ajax = new XMLHttpRequest();
  }
  return ajax;
}

function callback(ajax)
{
//如果执行是状态正常,那么就把返回的内容赋值给上面指定的层
  if (ajax.readyState == 4 && ajax.status == 200)
{
   show.innerHTML = ajax.responseText;
  }
else
{
alert("there was a problem retrieving the xml data:"+ajax.statusText);
}
}

function getNews(newsID)
{
 //如果没有把参数newsID传进来
 if (typeof(newsID) == 'undefined')
 {
  return false;
 }
 //需要进行Ajax的URL地址
 var url = "show.php?id="+ newsID;
 //获取新闻显示层的位置
 var show = document.getElementById("show_news");
 //实例化Ajax对象
 var ajax = InitAjax();

 //使用Get方式进行请求
 ajax.open("GET",url,true);
 //获取执行状态
 ajax.onreadystatechange =function() {
if (ajax.readyState == 4 && ajax.status == 200)
{
   show.innerHTML = ajax.responseText;
  }
}
 //发送空
 ajax.send(null);
}

//_______全选择__________
function SelectAll()
{
var empty;
var f = document.forms[0];
for (var i = 0; i {
empty = f[i];
if (empty.type == "checkbox" && empty.disabled == false)
empty.checked = true;
}
}

//__________返选择_________
function SelectReverse()
{
var empty;
var f = document.forms[0];
for (var i = 0; i {
empty = f[i];
if (empty.type == "checkbox" && empty.disabled == false)
if(empty.checked == true)
{
empty.checked = false;
}
else
{
empty.checked = true;
}
}
}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles