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$=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.
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.
<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
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.
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.
Alternatively, you can go for a Validation rule OR Javascript to validate that it must have a value. this also solves your objective.
<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..!