电子邮件验证使用正则表达式不适用于jsf?

时间:2013-12-07 06:45:18

标签: javascript jquery regex jsp jsf

that is my lib
我在我的jsp页面上使用JSF 2.1。我在点击忘记密码DIV时出现的DIV标记中包含了 h:inputText h:commandButton 。我想验证客户端本身的有效电子邮件地址的输入字段,所以我使用了 f:validateRegex 和“validatorMessage”,但是当我给出错误的电子邮件地址时它仍然无法正常工作说“123gmail.com”或提交它而不输入任何文字。它只是重新加载页面。任何人都可以帮助我......

这是我编辑的JSP页面代码供参考

<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@page import="com.fla.mbeans.ForgotPass"%>
<html>
  <head>
</head>
<body>
       <div style="left: 44%;top: 6%;width: 250px; position: relative;padding:20px;cursor: pointer" onclick="copytext()">
                <span id="fstyle"><ul>Forgot Password?</ul></span>
       </div>
 <div  id="fgpass" >

                    <div id="newpass">
                        <div style="position: absolute;top: 10%;left: 10%;">
                            <center><h2>PASSWORD RECOVERY</h2></center>
                            <h4> Enter Your Registered</h4> 


                  <h4>E-Mail Address :</h4>

                    <f:view>
                        <h:form>
                         <center>
                             <h:inputText id="cmailid" required="true" value="#{forgotPass.mailid}" validatorMessage="Invalid email">
                                 <f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]"/>
                             </h:inputText>
                             <h:commandButton value="Submit" action="#{forgotPass.fpass}">

                            </h:commandButton>



                         </center>

                   </div>
                    </div>

                            </h:form>
                </div>
 </body>
  </f:view>
</html>

我的Css

#fgpass {
    position: absolute;
    left: 65%;
    top: 4%;

    border-color: #009900;
    border-width: 3px;
    border-radius: inherit;
   z-index: 800;

}
#newpass{
   position: absolute;
   z-index: 1000;
   height:160px;
   width: 250px;
  color: midnightblue;

   visibility: hidden;
   background-color: lavender;
   -moz-border-radius: 8px;
   border-radius: 8px;
   border-width: 3px;
   border-color:#444;
}  

我的js

function copytext()
{
    var unhide=document.getElementById('newpass');
    src=document.getElementById('mailid');
    unhide.style.visibility='visible';
    dest=document.getElementById('cmailid');
    dest.value = src.value;
}

任何人都可以告诉我为什么它不起作用或我可以在客户端进行验证的任何其他方式.....我是新手,请帮助(谢谢)

1 个答案:

答案 0 :(得分:0)

使用jsf中的f validateRegex进行电子邮件验证。

使用模式^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$进行电子邮件验证。

示例:

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<f:view>
    <style type="text/css">
        .error {
            color: red;
        }
        .info {
            color: green;
        }
    </style>
    <h:form id="regexForm">
        <div>
            <h:messages id="messagesId" errorClass="error" infoClass="info"></h:messages>
            <h:inputText id="primaryEmailId" validatorMessage="Invalid PrimaryEmail [ Ex: udaykiran.pulipati@gmail.com ]" required="true"
                requiredMessage="Please Enter PrimaryEmail">
                <f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$"></f:validateRegex>
            </h:inputText>
        </div>
        <div>
            <h:commandButton value="Submit"></h:commandButton>
        </div>
    </h:form>
</f:view>