0

for email validation I have these two input fields:

<input class="inputbox" type="text" name="data[subscriber][email]" id="field_email" size="40" value="'.$this->escape(@$this->subscriber->email).'"/>

<input class="inputbox" type="text" name="data[subscriber][retype_email]" id="field_retype_email" size="40" value="" onblur="confirm_email()" />

the name of the input fields was different ( input1_name, input2_name ), and I had working script:

<script type = "text/javascript">
function confirm_email() {
email = document.adminForm.input1_name.value;
re_email = document.adminForm.input2_name.value;
if ( email != re_email ) {
alert ("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
document.adminForm.input1_name.value = "";
document.adminForm.input2_name.value = "";
document.adminForm.input1_name.focus();
}
else {}
}
</script>

BUT I NEED to access the element by id, because the input element needs to be formatted with brackets (this does not work in javascript). And so I derived the following script:

<script type="text/javascript">
function confirm_email(){
    var email = document.getElementById('field_email');
    var re_email = document.getElementById('field_retype_email');
    if( email != re_email )
        alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email') = "";
        document.getElementById('field_email').focus();
    else {} 
}
</script>

IT DOESN'T WORK

If I only use this part of the script it works:

<script type="text/javascript">
    function confirm_email(){
        var email = document.getElementById('field_email');
        var re_email = document.getElementById('field_retype_email');
        if( email != re_email )
            alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
            else {} 
    }
    </script>

which means that these lines are wrong:

        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email') = "";
        document.getElementById('field_email').focus();

but, I want the input elements to reset, and the first input element to be on focus (get the effect that I got when I used the first snippet of code that worked).

How can I do this with the getElementById method?

Thanx

1
  • 1
    Related: if( email != re_email ) is comparing the elements, which will always be different, you want if( email.value != re_email.value ) Mar 25, 2011 at 3:09

2 Answers 2

2

You're missing some braces, and some .value:

<script type="text/javascript">
function confirm_email(){
    var email = document.getElementById('field_email').value;
    var re_email = document.getElementById('field_retype_email').value;
    if( email != re_email ) {    //  <-- here
        alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email').value = "";
        document.getElementById('field_email').focus();
    }    // <-- and here
    else {} 
}
</script>
1

You are missing some .value attributes in your script. Does this work for you?

<script type="text/javascript">
function confirm_email(){
    var email = document.getElementById('field_email').value;
    var re_email = document.getElementById('field_retype_email').value;
    if( email != re_email ) {
        alert("ERROR: El correo electronico no coincide. Escribe tu correo electronico nuevamente")
        document.getElementById('field_email').value = "";
        document.getElementById('field_retype_email').value = "";
        document.getElementById('field_email').focus();
    }
    else {} 
}
</script>
1
  • NOTE: I added some curly braces too per Andrew's answer :)
    – dana
    Mar 25, 2011 at 3:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.