JavaScript: control pentru numere

Aceasta functie nu permite introducerea intr-o caseta de text decat a numrelor. Daca se va incerca sa se introduca litere, semne de punctuatie nu se va afisa in caseta de text nimic. Este utila pentru campurile de data sau de numere:

function TECtrlDigit()
{
if ((event.keyCode<48)||(event.keyCode>57))
return false;
return true;

}

sau

function CheckNumeric() {
// Obtains the ASCII value of key that was pressed
var key = window.event.keyCode;
if ( key > 47 && key < 58 )
return;
else
window.event.returnValue = null;
}

<input type=text name=”date” value=”” maxlength=”2″ size=”2″ onKeyup=’NextBox(“date”,”2″,”month”)’>/ <input type=text name=”month” value=”” maxlength=”2″ size=”2″ onKeyup=’NextBox(“month”,”2″,”year”)’>/ <input type=text name=”year” value=”” maxlength=”4″ size=”4″ onKeyup=’NextBox(“year”,”4″,”Conferma”)’>

function isDigit (field){
var valid = “0123456789”;
var ok = “yes”;
var temp;
for (var i=0; i<field.value.length; i++) {
temp = “” + field.value.substring(i, i+1);
if (valid.indexOf(temp) == “-1”)
ok = “no”;
}
if (ok == “no”) {
alert(“Valore non valido! Per questo campo sono ammessi solo numeri!”);
field.focus();
field.select();
return false;
}
return true;
}

function changeFocusOffset (InputObj, InputLen, offset) {
if (InputObj.value.length == InputLen) {
var i;
for (i = 0; i < InputObj.form.length; i++)
if (InputObj.form.elements[i].name == InputObj.name)
break;
i += offset;
if (i < InputObj.form.length)
InputObj.form.elements[i].focus();
}
}

function NextBox(objName,ObjValueLen,nextObjectName) {
if (event.keyCode) keycode=event.keyCode;
else keycode=event.which;

if (keycode !=9 && keycode !=16 && document.test.elements[objName].value.length==ObjValueLen) {
document.test.elements[nextObjectName].focus();
}
}
<input type=text name=”dueDateDay” value=”<%=dueDateDay%>” maxlength=”2″ size=”2″ onkeypress=”CheckNumeric()” onKeyup=’changeFocusOffset(this,2,1)’>/
<input type=text name=”dueDateMonth” value=”<%=dueDateMonth%>” maxlength=”2″ size=”2″ onkeypress=”CheckNumeric()” onKeyup=’changeFocusOffset(this,2,1)’>/
<input type=text name=”dueDateYear” value=”<%=dueDateYear%>” maxlength=”4″ size=”4″ onkeypress=”CheckNumeric()” onKeyup=’changeFocusOffset(this,4,1)’>

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 )

Google+ photo

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

Connecting to %s


%d bloggers like this: