regex - validate text field using regular expression javaScript -


i want validate text field accept text :

  • 1,2;2,3;1-3
  • 1-2;4;2,3;4;1-3
  • 12

i don't want types :

  • ;1
  • ,1
  • -1
  • 1;;2
  • 1,,2
  • 1--2
  • 1-2-3
  • 1,2,3
  • 1,2-3

so make regular expression seems doesn't work want

var reg = /^\d*(((?!.*--)(?!.*,,)(?!.*;;)(?!.*,;)(?!.*,-)(?!.*-;)(?!.*-,)(?!.*;,)(?!.*;-))[,-;])*\d$/ 

thanks :)

you can use regex

function match(str){      return str.match(/^(?!.*([-,])\d+\1)(?!.*,\d+-)\d+(?:[-,;]\d+)*$/) != null  }    console.log(match(';1'));  console.log(match(',1'));  console.log(match('1;;2'));  console.log(match('1-3'));  console.log(match('12'));  console.log(match('1,2;2,3;1-3'));  console.log(match('1-2;4;2,3;4;1-3'));  console.log(match('1,2,3'));

take @ regex demo


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -