使用正则表达式进行电子邮件验证在jsf中不起作用?

3

这是我的库
我正在使用JSF 2.1编写我的jsp页面。我在DIV标签中包含了一个h:inputText和一个h:commandButton,当点击忘记密码DIV时它们会出现。我想在客户端验证输入字段的有效电子邮件地址,因此我使用了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;
}

有人可以告诉我为什么它不起作用或者其他的方法在客户端进行验证吗?...我对这个很新,请帮忙(谢谢)


你有哪些JSF JAR文件?令人惊讶的是,看到已经被弃用的JSP而不是其继任者Facelets被使用。这表明你实际上正在使用JSF 1.x,这就解释了<f:validateRegex>根本没有起作用的问题。 - BalusC
@BalusC,您是最棒的伙伴......我已经上传了我使用的库的图片,请帮帮我,我对这些都很新,并且正在做我被告知要做的事情......只有在您提到之后,我才知道JSP已经过时了......我希望这张图片能帮助您找到我的问题,请帮帮我或者给我一个替代方案......我将非常感激您......再次感谢您的回复:)) - Vicky
仅供参考,_@9.aB% 并不是一个合法的电子邮件地址,但它能通过这个正则表达式。请尝试: [a-zA-Z0-9]([\w\.-]*[a-zA-Z0-9]+)+@[a-zA-Z0-9]([a-zA-Z0-9\.-]*[a-zA-Z0-9]+)+\.[a-zA-Z]{2,9} 或者添加 ^$\b - Suamere
@Suamere 谢谢,我会尝试并且回复你的。 - Vicky
使用正则表达式验证电子邮件通常不是一个好主意。这里有一个页面,展示了解析所有有效的RFC822电子邮件地址的正则表达式:http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html - 我不建议复制粘贴。 - Tom Whittock
1个回答

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接