Check if input is number or letter javascript

0 votes

HTML and javascript forms are what I'm utilizing. 

Only if the user enters a LETTER and hits submit do I want an alert to appear.

I have the HTML code here:

<form name="myForm" action="" onsubmit="return checkInp()" method="post">
    First name: <input type="text" name="age">
<input type="submit" value="Submit">   

And the javascript code:

function checkInp()
{
var x=document.forms["myForm"]["age"].value;
if (x consists of any letters) // this is the code I need to change
{
alert("Must input numbers");
return false;
}
}
Nov 14, 2022 in Java by Nicholas
• 7,760 points
1,023 views

1 answer to this question.

0 votes

The isNaN function can be used to determine whether or not a value converts to a number. Here's an example:

function checkInp()
{
  var x=document.forms["myForm"]["age"].value;
  if (isNaN(x)) 
  {
    alert("Must input numbers");
    return false;
  }
}
answered Nov 15, 2022 by Damonlang
• 1,230 points

Related Questions In Java

0 votes
0 answers

Check if a value is an object in JavaScript

How do you check if a value ...READ MORE

Dec 7, 2022 in Java by Nicholas
• 7,760 points
217 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,120 points
3,375 views
0 votes
3 answers

How to check whether a string is empty or not? Is there a function for this?

str != null && str.length() != 0 alternatively str ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,910 points
1,003 views
0 votes
1 answer

Check whether the string is palindrome or not?

You may refer the below code: public static ...READ MORE

answered Jul 11, 2018 in Java by Akrati
• 3,190 points
890 views
0 votes
0 answers

Expand form based on radio selection in jQuery & Bootstrap

I am trying to show a part ...READ MORE

Jul 22, 2022 in Web Development by gaurav
• 23,260 points
441 views
0 votes
0 answers

How to do calculations in HTML forms using JavaScript?

I'm developing a straightforward app that multiplies ...READ MORE

Jul 26, 2022 in HTML by Tejashwini
• 3,820 points
1,286 views
0 votes
1 answer

Best Practice: Access form elements by HTML id or name attribute?

It is a good idea to use ...READ MORE

answered Aug 8, 2022 in HTML by Vikas
679 views
0 votes
1 answer

How to get current formatted date dd/mm/yyyy in Javascript and append it to an input

The stringent equality operator (===) works exactly like the abstract equality operator (==), with the exception that no type conversion is performed, and the types must be the same to be regarded equal. Javascript Tutorial on Comparison Operators After performing any required type transformations, the == operator will check for equality.  Because the === operator does not perform the conversion, it will simply return false if two values are not of the same type.  Both are equally fast. "abc" == new String("abc") ...READ MORE

answered Nov 4, 2022 in Java by Damonlang
• 700 points
1,083 views
0 votes
1 answer

Check if a variable is a string in JavaScript

This is what I've found to be ...READ MORE

answered Nov 15, 2022 in Java by Damonlang
• 1,230 points
287 views
0 votes
1 answer

Violation Long running JavaScript task took xx ms

These messages are classified as warnings rather ...READ MORE

answered Nov 15, 2022 in Java by Damonlang
• 1,230 points
3,403 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP