javascript - allow minus sign in a regular expression -


i have regular expression, not know how allow minus sign mean, allow users use sign, can positive value without minus sign.

at beginning had regexp in javascript

 var regex = /[0-9]|\./; 

i've tried var regex = /\-[0-9]|\./; not work

this javascript function

function isnumeric(evt,type)  {   var theevent = evt || window.event;   var key = theevent.keycode || theevent.which;   key = string.fromcharcode( key );   if(type==1)   var regex = /[0-9]/;   else if(type==2)   var regex =  /^-?[0-9]\d*(.\d+)?$/;   else if(type==3)    var regex = /[0-9]|\ /;   /* 13=entrer;9=tabulation;8et 46 supprim� et sup; 37et 39 fl�che*/   if(  theevent.keycode == 13 || theevent.keycode == 9 ||  theevent.keycode == 8 || theevent.keycode == 46 || theevent.keycode == 37 || theevent.keycode == 39 || (theevent.keycode == 19 && type==3))   {   theevent.returnvalue = true;   }   else if( !regex.test(key))    {     theevent.returnvalue = false;     if(theevent.preventdefault) theevent.preventdefault();   }  } 

and how call it

onkeypress="isnumeric(event,2);" 

anykind of appreciated

you can make minus sign optional using ?:

var regex = /^-?[0-9]+(\.[0-9]+)?$/; 

i'm not sure why have |\. allows period in input.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -