日期:2014-05-16  浏览次数:20426 次

登陆/注册jsp页面用jQuery控制例子

js文件jQuery配置

jQuery(document).ready( function() {
??? jQuery("#loginForm").submit( function() {
??? ??? var canSubmit = new Boolean(true);
??? ???
??? ??? if (jQuery("#email").val() == "") {
??? ??? ??? jQuery("#emailErrorLogin").text(jQuery.l10n.__("Your login name is not meet E-mail format") );
??? ??? ??? canSubmit = false;
??? ??? }else
??? ??? ??? jQuery("#emailErrorLogin").empty();
??? ??? if (jQuery("#password").val() == "") {
??? ??? ??? jQuery("#passwordErrorLogin").text(jQuery.l10n.__("Password is a requested field!") );
??? ??? ??? canSubmit = false;
??? ??? } else
??? ??? ??? jQuery("#passwordErrorLogin").empty();

??????? //判断邮箱格式

??????? var emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
??? ??? if (jQuery("#email").val() == "") {
??? ??? ??? jQuery("#emailError").text(jQuery.l10n.__("E-Mail is a requested field!") );
??? ??? ??? canSubmit = false;
??? ??? } else if (!emailReg.test(jQuery("#email").val())) {
??? ??? ??? jQuery("#emailError").text(jQuery.l10n.__("Your E-Mail format is incorrect!") );
??? ??? ??? canSubmit = false;
??? ??? } else
??? ??? ??? jQuery("#emailError").empty();
??? ??? ??? return canSubmit;
??? ??? });

});

?

jsp页面应用

<form id="loginForm" action="${pageContext.request.contextPath}/user/login" method="post">
??? <table class="formtable" align="center">
??? ??? <tr>
??? ??? ??? <th><fmt:message key="user.email"/></th>
??? ??? ??? <td><input id="email" name="email"/></td>
??? ??? ??? <td id="emailErrorLogin"></td>
??? ??? </tr>

......

1 楼 miroku 2010-04-18  
jquery的验证插件不好吗??还要自己写??
2 楼 yuanyong 2010-04-28  
能用当然用啊
这几天我做的一个项目不用插件 提交页面的用JS验证还得给出相应提示
相当的无语 一个页面几百行JS