PDA

View Full Version : Filtered Textbox (REGEX)



dke01
04-04-2008, 02:38 AM
Hi,

I'm trying to only allow limited characters to be entered into a textbox using a regex I have the correct expression but cannot seem to get it to work?




<script language="javascript" type="text/javascript">


function myNumberFilter(txb) {

var filter = new RegExp("-{0,1}[0-9 \,]*");
if (!filter.test(txb.value)) txb.length -= 1;


}

</script>

<form>
<input type="text" id="test" onKeyUp="myNumberFilter(this);" />
</form>

Master_script_maker
04-04-2008, 07:55 PM
try:

var filter = new RegExp("[-]?[0-9 \,]*");

dke01
04-07-2008, 03:48 AM
Thanks I got it working with the following code



var re = new RegExp("-{0,1}[0-9]*$","gi");
var m = re.exec(txb.value);

if (m != txb.value)
txb.value = txb.value.substring(0, txb.value.length-1);

}