regex - Javascript regular expression,how to allows empty string for page value for printing -


i've regular expression check page value entered printing pages, 1-7 or 1-3,7 or 1-3,5-7. regex working cases, want allow blank page value, should not validate blank , print pages. i'm allowing user type special characters [&,-;_]. how allow blank or empty string regex. page value should start digit , end digit.

var pagevalue = document.getelementbyid("pages" + counter); var regex = /^\d([&,-;_]\d+)*$/ //if page value wrong, return null , display error message here  if(regex.test(pagevalue.value) == false) { alert("invalid page number: " + pagevalue.value);     return;   } 

i'm testing these page values. match found: 2-3 match found: 1,2 match found: 3-1 not match --- want match empty string match found: 1_2 not match abc match found: 1&2 match found: 1;2 not match -1-9 not match 1-9- match found: 1-5,13 match found: 1-5;13 match found: 1-5,13-23 not match 1-5,13-23;

you can use:

var regex = /^(\d([&,;_-]\d+)*)?$/ 

to allow blank accepted value.

ps: important keep - (hyphen) first or last in character class avoid escaping.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -