Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Javascript validtaion problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I need to validate a textbox using javascript so that the textbox accepts only numbers as its input.How to go about this using Javascript?Can anyone suggest a solution for this problem?A example would be much better.Thanks in advance.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've used something like this before, which is called on the onBlur of the field being validated:

 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or maybe you can call this function in the onkeypress event of the text box as checkNumeric(event).


dont know if it has any browser restrictions. if any do let me know.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Shashidahanu Iyyanar:
Hi All,
I need to validate a textbox using javascript so that the textbox accepts only numbers as its input.How to go about this using Javascript?Can anyone suggest a solution for this problem?A example would be much better.Thanks in advance.

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

var numb = "0123456789";

function chkInt()
{
txtVal = document.myform.numTxt.value;

if(txtVal != "")
{
for(i=0;i<txtVal.length;i++)
{
if(numb.indexOf(txtVal.charAt(i))== -1)
{
alert("Numbers Only !!");
break;
document.myform.numTxt.focus();
}
}
}

}

This is simple example for validating the numeric values for a text box

 
Shashidahanu Iyyanar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the timely help.
 
Wanna see my flashlight? How about this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic