cancel
Showing results for 
Search instead for 
Did you mean: 

how to validate a input filed?

Former Member
0 Kudos

hi

i have a input field with label mobile no which should accept only 10 didgits and no SPACE should be given between digits and input field shouldnot be even null,can u help me with the required code for this scenario

thanks

kishore

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

U need to create a string type attribute and bind it to a input field.

On Enter or on ur Submit button u can write following code.

I create a Attribute of string type say mobileno.


if(wdContext.currentContextElement().getMobileno().length()==10)//For calculating length.
	{
	  int k=2;
    
	for(int i=0;i<10;i++)//For  valid mobile no(0-9)
	{
			int j = wdContext.currentContextElement().getMobileno().charAt(i);
			if(j>=48 && j<=57)
			{
				k=1;
			}
			else
			{
			k=2 ;   		             
		   }
  		
	}
    if(k==2)
    {
    	 wdComponentAPI.getMessageManager().reportSuccess("Please Enter a valid number");
    } 
	else
	{
		//any custom code when no is valid
	}
  
	}
    
else
{
wdComponentAPI.getMessageManager().reportSuccess("Please Enter a valid number");
}

Regards

Surender

Former Member
0 Kudos

hi surendar,

thanks for u reply,my problem is that the inpufield shouldnot accept SPACE , for example if i enter 9 digits and in 10 th position if i enter space an error msg should be shown to me.

thanks

kishore

Former Member
0 Kudos

Hi

The above code will work because Space ascii value is 32.

regards

Surender Dahiya

Former Member
0 Kudos

hi surender

if i enter a null value the iam getting null pointer exception

can tell me the problem

thanks

kishore

nikhil_bose
Active Contributor
0 Kudos

put surendar's code inside else


if ( wdContext.currentContextElement().getMobileno() == 0 ) {

// display empty error message

}
else {

//put surendars code here...

}

nikhil

Edited by: Nikhil Bos on Jun 24, 2008 12:55 PM

Former Member
0 Kudos

HI surender

If my input field is empty iam getting null pointer exeption even tough i wrote the code (wdcontex.currentcontexelement().getmobibe().length==0)

thanks

kishore

former_member197348
Active Contributor
0 Kudos

Hi Kishore,

Check like this:

if(wdcontex.currentcontexelement()!=null && 
wdcontex.currentcontexelement().getmobibe()!=null)
{
//your code
}

Regards,

Siva

Former Member
0 Kudos

Hi,

Durga and Kishore...

use the following code to execute properly....


if(wdContext.currentContextElement().getMobileno()==null)
{
// Error message Please enter some value.
}
else
{
// Paste the full code from my previous post.
}

Regards

Surender Dahiya

Former Member
0 Kudos

Hi Kishore,

use the following code

if( wdcontext.currentcontextElement.getMobileNo()!=null){

String strMobileNumber = wdcontext.currentcontextElement.getMobileNo();

if ( strMobileNumber.indexOf((int)' ') != -1){

// say message "Enter correct mobile number"

} else {

// mobile number is a valid number

}

} else {

// say message "Enter mobile number"

}

Answers (4)

Answers (4)

venkatakalyan_karanam
Active Contributor
0 Kudos

Hi

Use the following method

public boolean isAllNumeric( java.lang.String stringValue )

{

//@@begin isAllNumeric()

boolean isNumeric = false;

isNumeric = Pattern.matches("[0-9]+", stringValue);

return isNumeric;

//@@end

}

which gives you whether the value is numeric or not.if not throw an error

if yes find the length of the string if not equals 10 throw an error.

Also you can restrict the input field to 10 digits by writing this code in init method.

use try catch block

ISimpleTypeModifiable homeAddrPostalCode = wdContext.wdGetAPI().getModifiableTypeOf ("HomeAddData<nodename>.PostalCode<attribute>");

homeAddrPostalCode.setMaxExternalLength(10);

Regards

Kalyan

Former Member
0 Kudos

Hi,

The following code should help you with your requirements.

if(wdContext.currentContextElement().getMobileNo().length()==10)//For calculating length.

{

int valid_flag=1;

for(int i=0;i<10;i++)

{

int j = wdContext.currentContextElement().getMobileNo().charAt(i);

if(j >=48 && j<=57)

{

valid_flag=1;

}

else

{

valid_flag=2 ;

break;

}

}

if(valid_flag==2)

{

wdComponentAPI.getMessageManager().reportSuccess("Please Enter a valid number");

}

else

{

wdComponentAPI.getMessageManager().reportSuccess("Valid number");

}

}

else

{

wdComponentAPI.getMessageManager().reportSuccess("Please Enter a valid number");

}

Regards,

Sudeep

lokesh_kamana
Active Contributor
0 Kudos

Hi,

Plz try this code

if (wdContext.currentPhoneNoElement().getPhoneNo() > 12)

{

wdComponentAPI.getMessageManager().reportException("Invalid Phone No", true);

}

else if (wdContext.currentPhoneNoElement().getPhoneNo() < 10)

{

wdComponentAPI.getMessageManager().reportException("Invalid Phone No", true);

}

Here the screnario waht i have taken is .

If the user enters a phone no. 919948222298.

Or if he enters 9948222298.

These 2 cond are validated by me.

Thanks & Regards,

Lokesh.

former_member197348
Active Contributor
0 Kudos

Hi Kishore,

Create a simpleType of Integer. Go to tab Representation. In the format field fill 9999999999 (10 9's) and External length field enter 10. And save it. Create a context attribute of that simpleType. And bind this attribute to your Inputfield (mobhile no). Validation is taken care by framework itself

Regards,

Siva

Edited by: Siva Rama Krushna on Jun 23, 2008 3:26 PM