• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regular expression validation script

 
mohan kumar r
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i want to add validation for text field so that user can enter only comma seperated list of digits.

i have added the following validation
-------------------------------------------------------------------
if ( !(/^[0-9]*[0-9,]*[0-9]$/.test(document.myForm.myfield.value )))
---------------------------------------------------------------------
but it accepting input of the form 2,3,4,,5,,6 which is invalid.it should not accept comma followed by comma.
is it possible to do by using reguler expression.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* means it must occur or more times. Do you see the problem in your code?

You should be doing something more like

(\d+,)*\d+

Eric
 
mohan kumar r
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my requirement is that ' it should be comma separated list of digits'. and duplicate commas are not allowed.

i have also tried like this:
--------------------------
if(!(/^[0-9]*[0-9]*[,]?[0-9]*$/.test(document.myform.mytext.value)))
{
alert("not valid");
document.myform.mytext.focus();
}
-----------------------------
but this too is not working
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try mine?

Eric
 
mohan kumar r
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Eric, it is working fine
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic