1

I have a simple VF form and i would like to validate the email address field when the user click on Submit.

<apex:page id="loginPage" showHeader="false" controller="SiteRegisterController" title="{!$Label.site.site_login}">
    <apex:define name="body">  
      <center>
        <apex:panelGrid bgcolor="white" columns="1"> 
          <br/>
          <br/>
          <apex:panelGrid width="758" cellpadding="0" cellspacing="0" bgcolor="white" columns="1" styleClass="topPanelContainer"> 
            <br/>
            <apex:outputPanel layout="block" styleClass="topPanel">
              <apex:panelGrid width="758" cellpadding="0" cellspacing="0" bgcolor="white" columns="2"> 
                <apex:panelGroup >
                  <h1>Don't have an account?</h1>
                  <br/>
                  <h2>Enter your email address to register</h2>
                   <apex:form id="theForm" forceSSL="true">
                    <apex:panelGrid columns="2" style="margin-top:1em;">
                      <apex:outputLabel value="{!$Label.site.email}" for="email"/>
                      <apex:inputText required="true" id="email" value="{!email}"/>
                      <apex:outputText value=""/>
                      <apex:commandButton action="{!registerUser}" value="{!$Label.site.submit}" id="submit"/>
                    </apex:panelGrid> 
                    </apex:form>   
                </apex:panelGroup>
              </apex:panelGrid> 
             </apex:outputPanel>
          </apex:panelGrid> 
       </apex:panelGrid>
      </center>


      <br/>
    </apex:define> 
</apex:page>

How can i achieve this?

Thanks in advantage for any advice.

BR.

2

Personally, I like to do my validation client side, and avoid doing it in Apex server side. That requires some jQuery knowledge though. I would use the jQuery Validate plugin. Very easy to use. Just need to import the jQuery library and the Validate library.

<script src="/jquery/jquery.js"></script>
<script src="/jquery/jquery.validate.js"></script>
<script>
$(document).ready(function(){
     $("#yourFormID").validate({

          rules: {
               name: "required",
               email: {
                    required: true,
                    email: true,
               },
               comment: "required"
          }

     });
});
</script>

This ensures that the email is of proper form user@domain.com. There are lots of other rules and messaging options as well. Overall, a very easy plugin to use. Check out the documentation here

http://jqueryvalidation.org/

| improve this answer | |
  • Relying on client-side validation is not a good idea because it can be circumvented. Client-side is good for responsiveness and saving a postback, but user input should not be trusted until validated server-side. – Mike Chale Aug 12 '13 at 11:40
  • 2
    This is really just a validation of format of the email address, and in no way prevents or restricts you from doing further validation server side. This is really just a matter of preference and certainly didnt warrant a down vote, so thanks for that. – Chris Duncombe Aug 12 '13 at 11:49
1

http://www.w3schools.com/js/js_form_validation.asp

You can try simple javascript validation for email listed in W3 school site

function validateForm(){

 var x=document.forms["theForm"]["email"].value;
 var atpos=x.indexOf("@");
 var dotpos=x.lastIndexOf(".");

  if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
       alert("Not a valid e-mail address");
      return false;
    }
}

<apex:inputText required="true" id="email" name="email" value="{!email}"/>

 <apex:commandButton action="{!registerUser}" onclick="return validateForm();" value="{!$Label.site.submit}" id="submit"/>
| improve this answer | |
  • Relying on client-side validation is not a good idea because it can be circumvented. Client-side is good for responsiveness and saving a postback, but user input should not be trusted until validated server-side. – Mike Chale Aug 12 '13 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.