几个简单的JS函数

1:自定义tirm()函数:String.prototype.trim = function(){ return this.replace(/(^\s*)|(\s*$)/g, "");}

调用如下:document.getElementById("name").name.value.trim();

2:Email邮箱验证:

var strP=/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
alert(strP.test(email.value.trim()));

3:手机号码验证:

var tmp = /^1[3-9]\d{9}|(\d{3}-)(\d{8})|(\d{4}-)(\d{7})|(\d{4}-)(\d{8})|$/;

alert(tmp.test(phone.trim()))

4:一个简单的时钟

window.onload=startTime;


function startTime(){
var time=new Date();
var hour=time.getHours();
var minute=time.getMinutes();
var second=time.getSeconds();
document.getElementById("timeSpan").innerHTML=hour+":"+minute+":"+second;
setTimeout("startTime()",500);
}

现在时间:<span id="timeSpan"></span>

5:表单提交控制

var flag={'fname':true,'fpassword':true,'frepwd':true};

function submitFunc(){

document.getElementById("password").onblur();
if(flag.fname==true&&flag.fpassword==true&&flag.frepwd==true)
return true;
return false;
}

<form action="infoSet.html" method="post" onsubmit="return submitFunc();">

<input type="submit" value="下一步"/>

</form>

你可能感兴趣的:(正则,trim,表单控制)