Allow number only in TextField by javascript

this example allow only number character in you text field.

—- JavaScript code

function numeralsOnly(thisComp,e,isDecimal) {
var key;
var keychar;
if (window.event) {
key = window.event.keyCode;
}
else if (e) {
key = e.which;
}
else {
return true;
}

// check double period/decimal/point
if (  ((thisComp.value).indexOf(‘.’) > -1) && key == 46){
alert(“Double period is not allow for this field.”);
return false;
}

var str = ’0123456789′;
str +=isDecimal ? ‘.’:”;
keychar = String.fromCharCode(key);
if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
return true;
}
else if (((str).indexOf(keychar) > -1)) {
return true;
}

alert(“Enter numerals only in this field.”);
thisComp.focus();
//thisComp.select();
return false;
}

——- html code

<input type=”text” id=”txtSkuPrice”  onkeypress=”return numeralsOnly(this,event,true);”/>

numeralsOnly(this,event,true) -> for Double number use true and for integer number use false.

Advertisement

One Response to Allow number only in TextField by javascript

  1. Tite says:

    Hi,

    When using WordPress, try to install “SyntaxHighlighter Evolved” plugin. It makes the code reading definitely more friendly.

    “Allow number only in TextField by javascript” can also be achieve quickly and with two/three lines of code by using regular expressions. Just give it a try!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.