+ 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.
dk alivedk alive
This is very useful information for me. Thank you very much!
Myxer Free Ringtones (https://myxerfreeringtonesdownload.com/) App Download 2020 ringback tones wallpapers for Android, iOS my Myxer Free Music Ringtones (https://myxerfreeringtonesdownload.com/free-music-ringtones-for-android/)
Michael Dean 25Michael Dean 25
Somesh Kumar 13Somesh Kumar 13
You can have a Javascript to check the require field & for proper email format you can use regression expression. Somesh Kumar  (http://someshkumar.in)
Monika JacobMonika Jacob
Thanks a lot for making this post, but I was looking for details on making validation on name using substr and instr. Would be really grateful for your assistance. Best Coursework Writing (https://www.theacademicpapers.co.uk/coursework-writing-service.php)
Bum AlexBum Alex
Thanks for your article, if you want you can also refer to the article of the same topic at: https://ringtonesdownloads.inf (https://ringtonesdownloads.info/)o/, I think you will love it as I liked your article.
Alessia MartineAlessia Martine

There are various subscriptions offered by the antivirus software Norton. Norton is capable of protecting all your handheld devices and computers. The antivirus software programs provide a comprehensive solution for all your needs related to cybersecurity. Moreover, the program offers a quick check for new viruses within every five to fifteen minutes, along with removing them. The innovative technology used in Norton prevents the keyloggers from tracking your online data. The wide range of Norton setup available at  www.norton.com/setup keep all your data safe, including files and applications.
Visit More:

norton.com/setup (https://publicblogs.co.uk/norton-setup)

norton.com/setup (https://my-blog-search.com/norton-setup/)

office.com/setup (https://internet-blogs.com/office-setup/)

mcafee.com/activate (https://ramcafee.webflow.io/)