You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sanjeewa Saman <sa...@mubasher.net> on 2008/02/27 07:49:26 UTC

Restric the values in S:TextFields.

Hi all ,

 

Can some body please tell me a way to restrict the data that we can eneter
to a textfield in struts2.

 

What I wanted to do is , for the s:textfield , I need to enter only values
these values {1-10 , +,-,.} .

 

Thank you.

sanjeewa


Re: Restric the values in S:TextFields.

Posted by Chris Pratt <th...@gmail.com>.
On Tue, Feb 26, 2008 at 11:12 PM, Sanjeewa Saman <sa...@mubasher.net> wrote:
> Yes Chris , that can be done using JS , But I thought that there should be a
>  way in struts2 to that.
>  May be some one have an idea of doing it.If so pls give ur idea to us.
>
I'm not sure what you think Struts can do on the Client side?  Since
you seem to not want to use JavaScript, that would put AJAX out of the
picture and the <s:textfield> tag just renders a standard HTML <input
type="text"> tag, so without JavaScript (or some wacky browser
specific CSS) there's really not much Struts can do.
  (*Chris*)

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Restric the values in S:TextFields.

Posted by Sanjeewa Saman <sa...@mubasher.net>.
Yes Chris , that can be done using JS , But I thought that there should be a
way in struts2 to that.
May be some one have an idea of doing it.If so pls give ur idea to us.

Tnx sanjeewa
 

-----Original Message-----
From: Chris Pratt [mailto:thechrispratt@gmail.com] 
Sent: Wednesday, February 27, 2008 12:29 PM
To: Struts Users Mailing List
Subject: Re: Restric the values in S:TextFields.

On Tue, Feb 26, 2008 at 10:49 PM, Sanjeewa Saman <sa...@mubasher.net>
wrote:
> Hi all ,
>  Can some body please tell me a way to restrict the data that we can
eneter
>  to a textfield in struts2.
>
>  What I wanted to do is , for the s:textfield , I need to enter only
values
>  these values {1-10 , +,-,.} .
>

The only way I know would be to use JavaScript in an onkeyup handler
that would check the entered value.
  (*Chris*)

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Restric the values in S:TextFields.

Posted by Laurie Harper <la...@holoweb.net>.
Chris Pratt wrote:
> On Tue, Feb 26, 2008 at 10:49 PM, Sanjeewa Saman <sa...@mubasher.net> wrote:
>> Hi all ,
>>  Can some body please tell me a way to restrict the data that we can eneter
>>  to a textfield in struts2.
>>
>>  What I wanted to do is , for the s:textfield , I need to enter only values
>>  these values {1-10 , +,-,.} .
> 
> The only way I know would be to use JavaScript in an onkeyup handler
> that would check the entered value.
>   (*Chris*)

If it's unacceptable for the user to even be able to type in 'invalid' 
characters then yes. If it's OK to let them type anything but then 
notify them after the fact that what they entered isn't acceptable, 
there are a few more options. If it's OK for that notification to be 
produced as part of the server-side validation when the form is 
submitted and redisplayed, it's as easy as using a regex validation rule.

So, sanjeewa, it depends on your exact requirements. I would start by 
adding in server-side validation (since that should be there anyway) 
then look at adding additional, client-side Javascript 
checking/reporting afterwards if you still feel you need it.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Restric the values in S:TextFields.

Posted by Chris Pratt <th...@gmail.com>.
On Tue, Feb 26, 2008 at 10:49 PM, Sanjeewa Saman <sa...@mubasher.net> wrote:
> Hi all ,
>  Can some body please tell me a way to restrict the data that we can eneter
>  to a textfield in struts2.
>
>  What I wanted to do is , for the s:textfield , I need to enter only values
>  these values {1-10 , +,-,.} .
>

The only way I know would be to use JavaScript in an onkeyup handler
that would check the entered value.
  (*Chris*)

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Restric the values in S:TextFields.

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Sanjeewa Saman wrote:
> Thank you very much Jeromy ,
> It  works I used the java Scripts for this to validate in client side.
>
>   
You're welcome.  Depending on how important the data is and how public 
the site is, also consider validating it on the server-side in case of 
users without javascript and the common idiot.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Restric the values in S:TextFields.

Posted by Sanjeewa Saman <sa...@mubasher.net>.
Thank you very much Jeromy ,
It  works I used the java Scripts for this to validate in client side.

sanjeewa

-----Original Message-----
From: Jeromy Evans [mailto:jeromy.evans@blueskyminds.com.au] 
Sent: Wednesday, February 27, 2008 1:44 PM
To: Struts Users Mailing List
Subject: Re: Restric the values in S:TextFields.

On the server-side, use an expression validator to ensure the input 
contains only valid characters.

On the client-side either:
 - use ajax validation; or
 - use a javascript function that listens for the keypress event and 
rejects invalid keystrokes

As usual, the javascript keypress event on IE is different from all 
other browsers so the function has to include browser detection.
Here is the gist of what you need:

<s:textfield name="x" onkeypress="filterKeypress()" ...>

javascript:
function filterKeypress(e) {
  var accept = /[\d|\+|\-|\.]/;   // a regex pattern of accepted chars
  var keyChar;
  if (msie) {  // use your favourite broswer detection function
     keyChar = String.fromCharCode(e.keyCode);
  } else {
    keyChar = String.fromCharCode(e.which);
  }
  // check if the character is acceptable
  if (!accept.test(keyChar)) {
      e.preventDefault();   // if it's not, prevent the keypress event 
from proceeding
  }
}


Sanjeewa Saman wrote:
> Hi all ,
>
>  
>
> Can some body please tell me a way to restrict the data that we can eneter
> to a textfield in struts2.
>
>  
>
> What I wanted to do is , for the s:textfield , I need to enter only values
> these values {1-10 , +,-,.} .
>
>  
>
> Thank you.
>
> sanjeewa
>
>
>   
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition. 
> Version: 7.5.516 / Virus Database: 269.21.1/1300 - Release Date:
26/02/2008 7:50 PM
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Restric the values in S:TextFields.

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
On the server-side, use an expression validator to ensure the input 
contains only valid characters.

On the client-side either:
 - use ajax validation; or
 - use a javascript function that listens for the keypress event and 
rejects invalid keystrokes

As usual, the javascript keypress event on IE is different from all 
other browsers so the function has to include browser detection.
Here is the gist of what you need:

<s:textfield name="x" onkeypress="filterKeypress()" ...>

javascript:
function filterKeypress(e) {
  var accept = /[\d|\+|\-|\.]/;   // a regex pattern of accepted chars
  var keyChar;
  if (msie) {  // use your favourite broswer detection function
     keyChar = String.fromCharCode(e.keyCode);
  } else {
    keyChar = String.fromCharCode(e.which);
  }
  // check if the character is acceptable
  if (!accept.test(keyChar)) {
      e.preventDefault();   // if it's not, prevent the keypress event 
from proceeding
  }
}


Sanjeewa Saman wrote:
> Hi all ,
>
>  
>
> Can some body please tell me a way to restrict the data that we can eneter
> to a textfield in struts2.
>
>  
>
> What I wanted to do is , for the s:textfield , I need to enter only values
> these values {1-10 , +,-,.} .
>
>  
>
> Thank you.
>
> sanjeewa
>
>
>   
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition. 
> Version: 7.5.516 / Virus Database: 269.21.1/1300 - Release Date: 26/02/2008 7:50 PM
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org