function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jay209jay209 

Making Field Mandatory with out Required=True

how make a field mandatory in visual force page without using "Required=true" for a standard controller
Best Answer chosen by jay209
jay209jay209
Thanks guys..!
<apex:page standardController="Account">
    <apex:form id="createAccount">
        <h1>New Account</h1><br/><br/>
        <apex:pageblock title="Add Account" id="thepageblock">
        <apex:pageblockSection id="thesection" >
        <apex:inputField value="{!Account.Name}" />
        <apex:inputField value="{!Account.Phone}" id="accountPhone"/>
        </apex:pageblockSection>
        <apex:pageBlockButtons Location="Bottom" title="click to save">
        <apex:actionFunction action="{!Save}" name="Mobileinserted"/>
        <apex:commandButton value="Save"  onclick="confirmation()" reRender="thesection"/>
        </apex:pageBlockButtons>
        </apex:pageblock>
    </apex:form>
    <script type="text/javascript">
        function confirmation()
        {
            var x = document.getElementById('{!$Component.createAccount.thepageblock.thesection.accountPhone}').value;
            if(x==null || x=='')
            {
               alert('Please Enter Phone..!!');
            }
            else
            {
                Mobileinserted();
            }
            
        }
    </script>
</apex:page>
i did like this its working..!

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Hi Jayaram,

Make the field required when you create the field. Or you can have a validation rule which will throw an error if the value of the field is null.
Hemant_SoniHemant_Soni
Hi Jayaram,
You have to use jQuery to make field Required and it is very easy way to make field required.
This is the example of jQuery
<apex:form id="submitform">
  <script type="text/javascript">
                jQuery(document).ready(function() {
                    jQuery('.file-wrapper input[type=file]').bind('change focus click', fileInputs);

                    jQuery('[id$=submitform]').each(function(){
                        jQuery(this).validate();
                    });
                    
                    jQuery('[id$=Name]').rules("add",{
                        required: true,
                     });
jQuery('#btnValidate').click(function(){
                        jQuery('[id$=submitform]').validate();
                        var form = jQuery('[id$=submitform]');
                        var j = form.valid()
                        });
});
</script>
//Your Code
<apex:commandButton action="{!save}" value="Validate" id="btnValidate"/>
</apex:form>
Please mark it solved if it help you.
jay209jay209
Thanks Hemant Soni 21 for your help..but it's not working
Abhilash Mishra 13Abhilash Mishra 13
Make it required at the time of creation will be the best option, as your field will also look required on VF page (with red block before it).
Alternatively, you can go for a Validation rule OR Javascript to validate that it must have a value.  this also solves your objective.
jay209jay209
Thanks guys..!
<apex:page standardController="Account">
    <apex:form id="createAccount">
        <h1>New Account</h1><br/><br/>
        <apex:pageblock title="Add Account" id="thepageblock">
        <apex:pageblockSection id="thesection" >
        <apex:inputField value="{!Account.Name}" />
        <apex:inputField value="{!Account.Phone}" id="accountPhone"/>
        </apex:pageblockSection>
        <apex:pageBlockButtons Location="Bottom" title="click to save">
        <apex:actionFunction action="{!Save}" name="Mobileinserted"/>
        <apex:commandButton value="Save"  onclick="confirmation()" reRender="thesection"/>
        </apex:pageBlockButtons>
        </apex:pageblock>
    </apex:form>
    <script type="text/javascript">
        function confirmation()
        {
            var x = document.getElementById('{!$Component.createAccount.thepageblock.thesection.accountPhone}').value;
            if(x==null || x=='')
            {
               alert('Please Enter Phone..!!');
            }
            else
            {
                Mobileinserted();
            }
            
        }
    </script>
</apex:page>
i did like this its working..!
This was selected as the best answer