admin管理员组

文章数量:1207738

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>

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>

Share Improve this question asked Aug 21, 2012 at 15:17 suvasuva 791 gold badge1 silver badge8 bronze badges 1
  • What would you try? Also, I don't think your currenct function really checks for duplicate values. – Bergi Commented Aug 21, 2012 at 15:20
Add a comment  | 

3 Answers 3

Reset to default 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).

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

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.

本文标签: javascriptComparing two input fieldsStack Overflow