`

JS验证email 和JQuery验证空值

阅读更多
1.  页面:join.jsp

<tr>
      <td align="right">Email:</td>
      <td align="left"><input id="email" type="text" name="email" value="" class="joinField"><br />
      <FONT color=red><span id="error_email"></span></FONT></td>
</tr>

 

2.  common.js

//email check
function emailCheck () {
	var emailStr = document.getElementById("email").value;
	var emailPat = /^(.+)@(.+)$/;
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) {
		document.getElementById("email").focus();  
		document.getElementById("error_email").innerHTML = "email is wrong !";
		return false;
	}else{
		return true;
	}
}

 

3. join.jsp提交按钮的时候,在button上面写事件。
onclick="return emailCheck();" 

 

 

在cyberage项目中的注册JS判断:

function registerCheckField () {
    var usernameValue = document.getElementById("username").value;   
    var passwordValue = document.getElementById("password").value; 
    usernameValue = usernameValue.replace(/\s/gi,"");
    passwordValue = passwordValue.replace(/\s/gi,"");
    var emailStr = document.getElementById("email").value;
    var emailPat = /^(.+)@(.+)$/;
    var matchArray = emailStr.match(emailPat);
    if(usernameValue !== "" && passwordValue !== ""){
    	document.getElementById("error_username").innerHTML = "";
    	document.getElementById("error_password").innerHTML = "";
    	if (matchArray == null) {
    		document.getElementById("email").focus();  
    		document.getElementById("error_email").innerHTML = "email format is incorrect !";
    		return false;
    	}else{
    		loginForm.action="./openid.servlet?signup=now";
    	    loginForm.sumbit(); 
    		return true;
    	}
    }else if(usernameValue == "" && passwordValue == ""){
    	document.getElementById("username").focus();
    	document.getElementById("error_info").innerHTML = "";
                document.getElementById("error_username").innerHTML = "username can't be null !";
                document.getElementById("error_password").innerHTML = "password can't be null !";
                return false;   
    }else if(usernameValue !== "" && passwordValue == ""){
    	document.getElementById("password").focus();
    	document.getElementById("error_info").innerHTML = "";
    	document.getElementById("error_username").innerHTML = "";
    	document.getElementById("error_password").innerHTML = "password can't be null !";
    	return false;
    }else if(passwordValue !== "" && usernameValue == ""){
    	document.getElementById("username").focus();
    	document.getElementById("error_info").innerHTML = "";
    	document.getElementById("error_password").innerHTML = "";
    	document.getElementById("error_username").innerHTML = "username can't be null !";
    	return false;
    }
}

 

$(function(){
		reg=/^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/; 
		$("#signupForm").submit(function(){
			if($("#username").val().replace(/\s/gi,"")=="")$("#un").html("<font color=\"red\">require field!!</font>");else $("#un").html("");
			if($("#password").val().replace(/\s/gi,"")=="")$("#psw").html("<font color=\"red\">require field!!</font>");else $("#psw").html("");
			if($("#email").val()=="")$("#em").html("<font color=\"red\">require field!!</font>");
			else if(!reg.test($("#email").val()))$("#em").html("<font color=\"red\">invalid email address!!</font>");
			else $("#em").html("");
			
			if(!reg.test($("#email").val())||$("#username").val()==""||$("#password").val()==""||$("#email").val()==""){
				return false;
			}
		});
})

 

分享到:
评论

相关推荐

    jQuery验证框架内置验证方法validate

    jQuery验证框架,即`jquery.validate.js`,是一款非常实用的JavaScript库,它为Web开发者提供了简单而强大的客户端表单验证功能。通过使用此插件,可以轻松实现各种复杂的验证逻辑,从而确保用户提交的数据符合预期...

    jquery.validate是jquery旗下的一个验证框架

    `jQuery Validate`支持多语言,通过引入相应的语言文件(如`jquery.validate.locale.js`)并设置`language`选项,可以实现验证错误消息的本地化。 ### 6. 错误消息显示 默认情况下,`jQuery Validate`会在输入框...

    html验证框架

    - JavaScript文件:包含了jQuery验证框架的核心代码,可能包括验证规则定义和处理逻辑。 - CSS文件:用于定义验证错误时的样式,如错误提示的显示位置和样式。 - HTML示例:展示了如何在HTML中使用该框架进行表单...

    html5,css3,jquery,js,php帮助文档chm格式合集

    jQuery还提供了许多插件,扩展了其功能,如表格排序、轮播图和表单验证等。 JavaScript是一种客户端脚本语言,用于实现网页的动态交互。在JavaScript中,可以创建对象、定义函数、操作DOM、处理事件、执行异步请求...

    基于jQuery的前端数据通用验证库

    总的来说,基于jQuery的前端数据通用验证库提供了一种简化前端验证流程的方法,通过在HTML中添加自定义属性,减少重复的JS验证代码编写,使得整个验证过程更加高效和直观。开发者可以根据项目的实际需求,灵活使用和...

    php程序员面试题(含html、JavaScript、php和mysql)

    **解析:**JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。JSON的基本数据结构包括对象(键值对的集合)和数组(值的有序集合)。 #### AJAX技术 *...

    贪玩php面试题.docx

    18. **验证邮箱格式**:使用`filter_var`函数配合`FILTER_VALIDATE_EMAIL`过滤器验证邮箱格式。 19. **去除HTML中的JS脚本**:使用正则表达式`/[^&lt;]*(?:(?!)&lt;[^&lt;]*)*&lt;\/script&gt;/i`匹配并替换掉。 20. **遍历目录**...

Global site tag (gtag.js) - Google Analytics