• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

regular expression validation script

 
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.
 
author
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
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
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic