0

I have two input fields and i am wondering how do i compare value's between these two fields.

<input id="start" type="numeric" value="" /> <input id="end" type="numeric" value="" /> 

In above input fields, if the value in input field with id='start' is greater than id='end', i want to display an alert.

I tried below but not working

 if ($("#end").val() > $("#start").val()) { //do something }else { alert('Wrong Input'); } 

What am i doing wrong???

4
  • type="number", As per my knowledge there is type as such numeric Commented May 24, 2014 at 8:06
  • you are missing a closing ) in the if statement Commented May 24, 2014 at 8:06
  • @Victory sorry typo mistake,but is correct in my code Commented May 24, 2014 at 8:07
  • When do you test the values? Is the code above part of a button or submit handler? Commented May 24, 2014 at 8:09

6 Answers 6

4

You should bind an event handler such as 'keypress' to one of the fields. When that even is triggered, you should compare the values of both the input fields and show alert if necessary.

Additionally, type="number" is correct not "numeric" .

Here's a working fiddle-

http://jsfiddle.net/pe2ZE/

Sign up to request clarification or add additional context in comments.

Comments

2

Use type="number", As per my knowledge there is type as such numeric

Code

if (+$("#end").val() > +$("#start").val()) { //do something } else { alert('Wrong Input'); } 

Here I have use + to convert value to integer

1 Comment

@Richa, It happens with all of us.
2

you are comparing strings $("#end").val() > $("#start").val() so you have to compare in numbers, and dont forget about the radix

if(parseInt($("#end").val(),10) > parseInt($("#start").val(),10)) 

and type="numeric" is a wrong syntax, use type="number"

<input id="start" type="number" value="" /> 

Comments

1

You need to use type="number" to make the script work:

<input id="start" type="number" value="" /> <input id="end" type="number" value="" /> 

Demo

Or you can use input type text and then parse the input using parseInt(val) and compare them. somethink like this:

if (parseInt($("#end").val()) > parseInt($("#start").val())){ //rest code } 

1 Comment

Thank a lot.Got it solved. That was some silly mistake
1

you can't use type="numeric" to make input numeric only

to solve this proplem use this code

HTML

<input type="tel" name="name">

jQuery

 // HTML Text Input allow only Numeric input $('[type=tel]').on('change', function(e) { $(e.target).val($(e.target).val().replace(/[^\d\.]/g, '')) }) $('[type=tel]').on('keypress', function(e) { keys = ['0','1','2','3','4','5','6','7','8','9','.'] return keys.indexOf(event.key) > -1 }) 

Comments

0

You have to type cast the value to int just like

if (parseInt($("#end").val()) > parseInt($("#start").val())) { //do something }else { alert('Wrong Input'); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.