Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Regular Expressions

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I am trying to do client side validations using javascript to only allow digits and % in the text box.
Can anyone please tell me how to do this ?
I tried to put the regular expression '[0-9%]' but I want to allow the user digits and % ?

Thanks,
Gayatri
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try
\d+(%)?

or
\d+(%)

Eric
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Eric.
Both did not work for me
This is what I say :

var pattern = '\d+(%)';
var matched = myfield.match(pattern);
if(matched != null) {
alert("invalid entry");
}

Any idea what is wrong here ? I want to allow only digits and % in myfield.

Thanks,
Gayatri
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to use / instead of '

another option:
var pattern = /\d{1,3}(%)/;

Eric
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying Eric.
var pattern = /\d{1,3}(%)/;
allows all alphabets, special characters like !@#$^&*(){}[]:"';<>?/\|
I need the user to be allowed to enter only digits and %. User should not be allowed to enter alphabets and special characters.

Thanks,
Gayatri
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic