0

I have a problem i have the following javascript function:

<script type="text/javascript">
<!--
function BothFieldsIdenticalCaseSensitive() {
    var two = document.bid_deal.passphrase_hidden.value;
    var three = document.bid_deal.passphrase.value;
    if(two == three) { return true; }
    alert("Warning!! passcodes must match!!!");
    return false;
}
//-->
</script>

I am using it to compare two text fields to make sure the two text fields match themselves. it is working for one set of text fileds but has refused to work if i add another set of text field and run another instance of the same function.

I have a call javascript function calling checkEnableSubmit when the action is executed but i dont know why it won't work with more than one set of text fields. I however noticed that both call javascript behaviors in the page call the same name of checkEnableSubmit. If i change the name one stops working.

HERE IS THE JAVASCRIPT CALL: <input name="passphrase" type="text" id="passphrase" onfocus="clearDefault(this)" onblur="MM_callJS('checkEnableSubmit')" value="Nopass" />

I need to implement this script to add validation to my text fields, please i need help BADLY!!!!

3
  • Could you add your html here ? Commented Jun 27, 2013 at 17:34
  • Add your html and also the code for checkEnableSubmit Commented Jun 27, 2013 at 17:41
  • Can you provide a little more context to the code? Like maybe a working/breaking demo in JSFiddle? Commented Jun 27, 2013 at 17:44

1 Answer 1

1

Your BothFieldsIdenticalCaseSensitive function can only work on the fields *passphrase_hidden* and passphrase.

If you wish to check another two fields, then you must create another function for new fields. If, for instance, you created two fields with ids *passphrase_hidden1*, and passphrase1 then you could use this function to validate their input...

function BothFieldsIdenticalCaseSensitive2() {
    var two = document.getElementById('passphrase_hidden1').value;
    var three = document.getElementById('passphrase1').value;
    if(two == three) { return true; }
    alert("Warning!! passcodes must match!!!");
    return false;
}

another option, that you could try, would be to create one single function and in that function pass the two fields that you want to check like so:

function BothFieldsIdenticalCaseSensitive(fieldId1, fieldId2) {
    var two = document.getElementById(fieldId1).value;
    var three = document.getElementById(fieldId2).value;
    if(two == three) { return true; }
    alert("Warning!! passcodes must match!!!");
    return false;
}

Edit

You shouldn't reference your fields with this syntax document.bid_deal.passphrase_hidden, not all browsers implement this. The standard way that works across all browsers is to call document.getElementById('passphrase_hidden').

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

2 Comments

Thanks for the replies. I also have an on submit behaviour to the form which i pasted in. Should this also be added when i call a new instance of this validation scipt
Hey again guys, thanks for the replies. Interestingly, the script above works for validation of select fields to ensure that entered values are identical. The only problem is that the form cannot be submitted. My aim is to ensure that the select fields have the same value always and not to prevent form submission if the select fields have the same value. Any help on this is greatly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.