validation code / function to allow only NUmbers in a text box

Answers were Sorted based on User's Feedback



validation code / function to allow only NUmbers in a text box..

Answer / shwetha kamath

function validate_user_editadvanced_form_username(element)
{var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<element.value.length; i++) {
temp = "" + element.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry! Only numbers are accepted!");
element.focus();
element.select();
}
}

Is This Answer Correct ?    9 Yes 1 No

validation code / function to allow only NUmbers in a text box..

Answer / kevin konkle

//Have this be called off the keypress event of your
textbox control.
function numberFormat(fld, e)
{
var key = '';

//This string contains all characters you want to allow
var strCheck = '0123456789';
var whichCode = (window.Event) ? e.which : e.keyCode;

if (whichCode == 13) return true; // Enter
if (whichCode == 8) return true; // Delete

// Get key value from key code
key = String.fromCharCode(whichCode);

// Not a valid key
if (strCheck.indexOf(key) == -1) return false;

//If it is a legal value then add it to the text box
fld.value += key;

return false;
}

Is This Answer Correct ?    6 Yes 2 No

validation code / function to allow only NUmbers in a text box..

Answer / satish reddy

function isNumeric(keyCode)

{

if(keyCode==16)

isShift = true;



return ((keyCode >= 48 && keyCode <= 57 || keyCode ==
8 ||

(keyCode >= 96 && keyCode <= 105)) && isShift
== false);

}

Is This Answer Correct ?    4 Yes 1 No

Post New Answer

More JavaScript Code Interview Questions

i am making a purchase sheet in html ,it contain names of product(by selecting checkboxes),quantity(textbox) and price(checbox), by using java script i am restricting the user to prevent them to enter the wrong data ,so i made the servlet were i am only able to prevent them to enter the page in empty field , so my doubt is how do you prevent them how to enter non numerical data in quantity field

2 Answers   Infosys,


code to calculate the number of days between two dates

2 Answers  


how to prevent an Event from performing its default behavior

0 Answers   CAE,


determine which Element received an Event

0 Answers  


How to Add Message and Confirmation Boxes Using JavaScript?

1 Answers  






validation code / function to allow only NUmbers in a text box

3 Answers   GE,


could you please tell me for what javac, javax,swing,awt.*; are using. what is the meaning and differences. and also there are 4 access modifiers in java. public, private, protected, default. what is the meaning of these 4 access modifiers and difference. public is the access modifier void means return data type disp() means function name what mean by static

1 Answers   Infosys,


code to detect availability of cookies

0 Answers  


snippet to prevent submission of form when certain/any validations got failed

0 Answers  


write a program to detect the user's browser ?

1 Answers   Infosys,


communication between main window and new windows

0 Answers  


write a function to validate a given date

1 Answers  


Categories
  • PHP Code Interview Questions PHP Code (33)
  • JSP Code Interview Questions JSP Code (6)
  • ASP Code Interview Questions ASP Code (5)
  • CGI Perl Code Interview Questions CGI Perl Code (3)
  • JavaScript Code Interview Questions JavaScript Code (63)
  • VB Script Code Interview Questions VB Script Code (20)
  • Shell Script Code Interview Questions Shell Script Code (31)
  • Python Code Interview Questions Python Code (34)
  • WinRunner Code Interview Questions WinRunner Code (1)
  • HTML DHTML XHTML Code Interview Questions HTML DHTML XHTML Code (13)
  • XML Interview Questions XML (43)
  • Scripts_Markup Code AllOther Interview Questions Scripts_Markup Code AllOther (5)