+ Start a Discussion
Alex Wong 4Alex Wong 4 

Is there a way to perform validation for apex:inputField?

I am creating a form in Visualforce. Since my form is a bit long, it is hard for users to remember the error message at the top and revise it. On top of the error message, I want to create a validation when users typing in their information.
For example, there are 2 field, users name and email. Users cannot leave the name field blank, if so, I want to show a message of "This field is required to fill in" beside the field. And for email field, I want to have another message to remind users to insert a valid email.
Is there anyway in visualforce to do so? Thanks for your help.
Best Answer chosen by Alex Wong 4
Aniket Malvankar 10Aniket Malvankar 10
Hello Alex,

You can add something like this:
1. Check the Empty
<apex:page id="page">
    
    <script>
    function show()
    {
        var name=document.getElementById('page:f1:p1:ip1').value;
        if(name== "" || name==null)
        {
            document.getElementById("page:f1:p1:op2").innerHTML = "Please enter your name"; 
        }
    }
    </script>
    
    <apex:form id="f1">
        <apex:pageblock id="p1">
            <apex:outputlabel value="Enter u r name" id="op1" />
            <apex:inputtext id="ip1" />
            <apex:commandbutton value="submit" onclick="return show();" reRender="op2"/>
            <apex:outputlabel id="op2" />
        </apex:pageblock>
    </apex:form>
    
</apex:page>

---> Besides salesforce has the on field validation, so u can have that option.

2. create formula syntax 
Regression : '([a-zA-Z0-9_\\-\\.]+)@(((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3}))'


Thanks
Aniket

 

All Answers

Aniket Malvankar 10Aniket Malvankar 10
Hello Alex.

You can have a Javascript to check the require field & for proper email format you can use regression expression.

Thanks
Aniket

* if you find the reply FINE. Please like
Aniket Malvankar 10Aniket Malvankar 10
Hello Alex,

You can add something like this:
1. Check the Empty
<apex:page id="page">
    
    <script>
    function show()
    {
        var name=document.getElementById('page:f1:p1:ip1').value;
        if(name== "" || name==null)
        {
            document.getElementById("page:f1:p1:op2").innerHTML = "Please enter your name"; 
        }
    }
    </script>
    
    <apex:form id="f1">
        <apex:pageblock id="p1">
            <apex:outputlabel value="Enter u r name" id="op1" />
            <apex:inputtext id="ip1" />
            <apex:commandbutton value="submit" onclick="return show();" reRender="op2"/>
            <apex:outputlabel id="op2" />
        </apex:pageblock>
    </apex:form>
    
</apex:page>

---> Besides salesforce has the on field validation, so u can have that option.

2. create formula syntax 
Regression : '([a-zA-Z0-9_\\-\\.]+)@(((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3}))'


Thanks
Aniket

 
This was selected as the best answer
Aniket Malvankar 10Aniket Malvankar 10
Use onfocusout instead onchange
Alex Wong 4Alex Wong 4
@Aniket Malvankar 10 actually, I recently found a link demonstrating what I want: https://th3silverlining.com/2010/03/02/visualforce-form-validation-enhanced/
However, nothing happen even I follow him. Is your method show the similar function to the link?
Thanks for your help!!!
Alex Wong 4Alex Wong 4
@Aniket Malvankar 10 I am trying your method now. Thanks.
Divyansh saxena 8Divyansh saxena 8
Thanx for this information    
Geektechboy.com
Rohit TantiRohit Tanti
Thank you boss
Newstatus.in (https://newstatus.in/sad-status-hindi/)
Jim HollJim Holl

This is very useful information for me. Thank you very much!

My work : homework for you (https://homeworkfor.me)

 

Inspire DogsInspire Dogs
very good information for dog lovers and dog training for fetch toys and return the toy to the dog owner dog training for fetch toys (https://inspiredogs.com/train-dog-fetch-return-the-toy-back/)
Stone JosephineStone Josephine
It's very good to know the answer. That is also my question too.
For this website: https://street-view.org/
 
Vishal SenuriaVishal Senuria
Nice post here is a useful app. The redbox app (https://redboxtv.buzz) is a free to use app for free movies.