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;
}
}
}

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)