您的位置:首页 > Web前端 > JavaScript

用户模块之js验证相关(包含正则)

2011-06-29 15:46 267 查看
用正则表达式对象的那三个方法去进行匹配
正则表达式在jscript中是以/XXX/形式的,两条/线之间的是正则表达式用test方法匹配
compile 方法,重新编译,即先前已经有的re对象重新编译,参数是新的正则表达式.

正则表达式,通过别人的例子来看,来学习.可以到google中搜常用正则表达

添加用户的js验证代码如下:

function addUser() {
var userIdField=document.getElementById("userId");
if(trim(userIdField.value)==""){
alert("用户代码不能为空");
userIdField.focus();
return;
}
/*
if(trim(userIdField.value).length<4){
alert("用户代码长度不能小于4");
uerIdField.focus();
return;
}*/
if(!(trim(userIdField.value).charAt(0)>='a'&&trim(userIdField.value).charAt(0)<='z')&&!(trim(userIdField.value).charAt(0)>='A'&&trim(userIdField.value).charAt(0)<='Z')){
alert("用户代码首字母必须为字母");
uerIdField.focus();
return;
}
//验证userId只能是数字或字母,长度为4-6位(不用正则)
/*if(!(trim(userIdField.value).length>=4&&trim(userIdField.value).length<=6)){
alert("用户代码长度只能为4-6位");
uerIdField.focus();
return;
}
for(var i=0;i<trim(userIdField.value).length;i++){
var c=trim(userIdField.value).charAt(i);
if(!((c>='a'&&c<='z') || (c>='A'&&c<='Z')|| (c>=0&&c<=9)) ){
alert("用户代码必须为字母或数字");
uerIdField.focus();
return;
}
}*/
//采用正则表达式验证userId只能是数字或字母,长度为4-6位
var re=new RegExp(/^[a-zA-Z0-9]{4,6}$/);
if(!re.test(trim(userIdField.value))){
alert("用户代码必须为4-6位的字母或数字");
uerIdField.focus();
return;
}
//用户名称不能为空
var userNameField=document.getElementById("userName");
if(trim(userNameField.value)==""){
alert("用户名称不能为空");
userNameField.focus();
return;
}
//密码长度至少6位
var passwordField=document.getElementById("password");
if(trim(passwordField.value).length<6){
alert("密码长度必须至少为6位");
passwordField.focus();
return;
}
//如果联系电话不为空,那么他必须为数字1,不用正则2用正则
var contactTelField=document.getElementById("contactTel");
//不用正则
/*if(trim(contactTelField.value)!=null){
for(var i=0;i<trim(contactTelField.value).length;i++){
var c=trim(contactTelField.value).charAt(i);
if(!(c>=0&&c<=9)){
alert("电话号码不合法");
contactTelField.focus();
return;
}
}
}*/
//2.使用正则
if(trim(contactTelField.value)!=null){
re.compile(/^[0-9]*$/);
if(!re.test(contactTelField.value)){
alert("电话号码不合法");
contactTelField.focus();
return;
}
}

//如果email不为空,那么他必须包含@,@不能在第一个和最后一个.
var emailField=document.getElementById("email");
var emailValue=trim(emailField.value);
if(emailValue!=""){
if(emailValue.indexOf("@")==0||emailValue.indexOf("@")==emailValue.length-1||emailValue.indexOf("@")<0){
alert("email不合法");
emailField.focus();
return;
}
}

if(document.getElementById("spanUserCode").innerHTML!=""){
alert("用户代码已经存在");
userIdField.focus();
return;
}
with(document.getElementById("userForm")){
action="user_add.jsp";
method="post";
submit();
}
}

常用的js验证:client_validate.js

//是否为空校验
function isEmpty(s) {
var lll=trim(s);
if( lll == null || lll.length == 0 )
return true;
else
return false;
}

//删除字符串左边的空格
function ltrim(str) {
if(str.length==0)
return(str);
else {
var idx=0;
while(str.charAt(idx).search(//s/)==0)
idx++;
return(str.substr(idx));
}
}

//删除字符串右边的空格
function rtrim(str) {
if(str.length==0)
return(str);
else {
var idx=str.length-1;
while(str.charAt(idx).search(//s/)==0)
idx--;
return(str.substring(0,idx+1));
}
}

//删除字符串左右两边的空格
function trim(str) {
return(rtrim(ltrim(str)));
}

/*日期相比较*/
function compareDate(date1, date2) {
if (trim(date1) == trim(date2))
return 0;
if (trim(date1) > trim(date2))
return 1;
if (trim(date1) < trim(date2))
return -1;
}

//校验是否是Email
function isEmail(eml) {
if(trim(eml)!='') {
var re=new RegExp("@[//w]+(+)+$]//.[//w]+)+$");
return(re.test(eml));
}
else
return(true);
}

//是否是电话号
function isTel(tel) {
var charcode;
for (var i=0; i<tel.length; i++)
{
charcode = tel.charCodeAt(i);
if (charcode < 48 && charcode != 45 || charcode > 57)
return false;
}
return true;
}

//校验是否是实数
function isnumber(num) {
var re=new RegExp("^-?[//d]*//.?[//d]*$");
if(re.test(num))
return(!isNaN(parseFloat(num)));
else
return(false);
}

//校验是否是整数
function isinteger(num) {
var re=new RegExp("^-?[//d]*$");
if(re.test(num))
return(!isNaN(parseInt(num)));
else
return(false);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: