4

I have this function which i am using to compare two input fields. If the user enters the same number in both the text field. On submit there will be an error. Now i would like to know if there is a way to allow same number but not higher than or lower the value of the previous text box by 1. For example if user enters 5 in previous text box, the user can only input either 4, 5 or 6 in the other input field.Please give me some suggestions.

 <script type="text/javascript"> function Validate(objForm) { var arrNames=new Array("text1", "text2"); var arrValues=new Array(); for (var i=0; i<arrNames.length; i++) { var curValue = objForm.elements[arrNames[i]].value; if (arrValues[curValue + 2]) { alert("can't have duplicate!"); return false; } arrValues[curValue] = arrNames[i]; } return true; } </script> 

<form onsubmit="return Validate(this);"> <input type="text" name="text1" /><input type="text" name="text2" /><button type="submit">Submit</button> </form> 

1
  • What would you try? Also, I don't think your currenct function really checks for duplicate values. Commented Aug 21, 2012 at 15:20

3 Answers 3

11

A tidy way to do it which is easy to read:

var firstInput = document.getElementById("first").value; var secondInput = document.getElementById("second").value; if (firstInput === secondInput) { // do something here if inputs are same } else if (firstInput > secondInput) { // do something if the first input is greater than the second } else { // do something if the first input is less than the second } 

This allows you to use the values again after comparison as variables (firstInput), (secondInput).

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

2 Comments

Do something if Second is greather than Second? I think you mean if firstInput < secondInput or not equal?
@LmC I need to check firstInput > secondInput but it only validating for 1st digit only how to fix it? Ex: 59>9 here it only checking 5>9
3

Here's a suggestion/hint

if (Math.abs(v1 - v2) <= 1) { alert("can't have duplicate!"); return false; } 

And here's the jsfiddle link, if you want to see the answer

5 Comments

@I pm I need to check firstInput > secondInput but it only validating for 1st digit only how to fix it?
You can do something like this in your validate function: if (('' + a)[0] <= ('' + b[0])) { alert('invalid'); return false; } Basically ('' + anyNumber)[0] will get you the first digit of a number.
@I pm Thank you for quick response..I didn't get you Once see my code here I wanna validate firstInput > secondInput I used this document.getElementById('noOfWorkingComputers').value > document.getElementById('noOfComputers').value but taking only validating for 1st digit only how to fix it? Ex: 59>9 here it only checking 5>9
here it's working fine for only single digit..If it checking like string even that single digit also not gonna be work naa?
Could you post a jsfiddle (or something similar) link? So it would be easier to look at your problem.
1

Give them both IDs.

Then use the

if(document.getElementById("first").value == document.getElementById("second").value){ //they are the same, do stuff for the same }else if(document.getElementById("first").value >= document.getElementById("second").value //first is more than second } 

and so on.

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.