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
sunny@99-chgsunny@99-chg 

To write Validation on input field in Visualforce

Hai,

I have one inputField in visualforce page.In that inputField I want to enter only Numbers of size four.

 

How to write validation in controller.please can anyone tell me...

vishwa attikerivishwa attikeri

HI,

Try this it may hepl you

 

public controller
{
String inputstring{get;set;}

Pagereference save()
{
Integer len = inputstring.length();

if(len>4)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, ' Lenght is greater than 4');
}
else
{
filed__c=inputstring;
}

 

 

<apex:page controller="Controller">
<apex:inputtext value="{inputstring}"/>
</apex:inputtext>

 

 

Jerun JoseJerun Jose

Another way that I thought would be to meddle with the setter method for that component

 

public controller
{
	String inputstring{get;}
	public void setinputstring(String value){
		if(value == null || value.length() > 4){
			ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, ' Length is greater than 4');
		}else{
			inputstring = value;
		}
	}
}

 

sunny@99-chgsunny@99-chg

Thanks for ur reply.

But here i had taken inputfield,i.e internally in database its type is number..

vishwa attikerivishwa attikeri

Do one small change here

 

field__c=inputstring; 
//instead of this use
field__c=Integer.valueof(inputstring); 

 

Jerun JoseJerun Jose
and you want to check if the number is greater than 9999 ?
Jerun JoseJerun Jose
Even still you could use these codes as a prototype and modify it to fit your logic