JSF: Email Address Not Stripping Spaces on Validation

来源:互联网 发布:淘宝最新市值 编辑:程序博客网 时间:2024/06/14 05:23

In the following script,    <f:validateRegex pattern="^[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" /> consider trailing spaces as invalid,


I have a task to make it valid, that is even there is trailing spaces, the input filed will pass the regex validation.


So I add this attribute:  onchange="this.value=this.value.trim()" .The onchange fires before the validations do, so it will act as if the user did not enter any spaces at the beginning or end of the input.


The reason that I didn't modify the regex pattern because I don't want backend work. Because the input value is bound with backend bean, bean or controller need to handle stripping if regex were changed.

 <!-- EMAIL AND EMAIL CONFIRM -->                    <h:panelGroup id="emailPanel" layout="block" styleClass="myatc-formrow-group">                        <atcui:formError id="emailMessage" for="enterEmail" ajaxRendered="false" />                        <atcui:formRow>                            <h:inputText id="enterEmail"                                         required="true"                                         value="#{loginSelectionBean.email}"                                         onchange="this.value=this.value.trim()"                                         maxlength="45"                                         pt:placeholder="email"                                         pt:autocapitalize="none"                                         requiredMessage="Please enter a valid email address."                                         validatorMessage="Please enter a valid email address. Example: yourname@domain.com">                                <f:validateRegex pattern="^[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />                            </h:inputText>                        </atcui:formRow>                        <atcui:formError id="confirmEmailMessage" for="confirmEmail" ajaxRendered="false" />                        <atcui:formRow>                            <h:inputText id="confirmEmail"                                         required="true"                                         value="#{loginSelectionBean.confirmEmail}"                                         onchange="this.value=this.value.trim()"                                         onchange="this.value=this.value.trim()"                                         maxlength="45"                                         pt:placeholder="reenter email"                                         pt:autocapitalize="none"                                         requiredMessage="Email confirmation is required."                                         validatorMessage="Please enter a valid confirmation email address.">                                <f:validateRegex pattern="^[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />                            </h:inputText>                        </atcui:formRow>                    </h:panelGroup>                    <!-- PASSWORD AND PASSWORD CONFIRM -->                                       </h:panelGroup>


0 0
原创粉丝点击