0

Sir, I am using these codes.

<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">

function checkEmail()
{

    var email = document.getElementById(xemail);

    alert(email);

    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email.value))
    {
    alert('Please provide a valid email address');
    email.focus;
    return false;
    }
}

</script>

<tr>
<td width="40"><label>Email</label></td>
<td width="40">
 <input type="text" name="email" id="xemail" value="<?php echo $myemail; ?>" onblur="checkEmail()">
 </td>
</tr>

It always display this result:

[IMG]http://i43.tinypic.com/2e3911i.jpg[/IMG]

Textbox contains value but it says: Null

What I am doing wrong?

Edited by tqmd1

4
Contributors
3
Replies
32
Views
3 Years
Discussion Span
Last Post by Airshow
0

Your first issue is that you need to correct line #7. Change it to...

var email = document.getElementById("xemail").value;

1

...that should fix it (JorgeM's advice)
and here is a ready to copy-paste (corrected) script...

<script type="text/javascript">

function checkEmail()
{

    var email = document.getElementById('xemail');

    //alert(email);

    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email.value))
    {
    alert('Please provide a valid email address');
    email.focus;
    return false;
    }
}

</script>

Edited by Troy III

0

tqmd1,

Two points:

  1. You have jQuery available, so you might as well use it
  2. You may want to return true on success. This probably doesn't matter but it's good practice given that you return false on failure.

Like this:

function checkEmail() {
    var $email = $("#xemail");
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test($email.val())) {
        alert('Please provide a valid email address');
        $email.focus();
        return false;
    }
    return true;
}

Edited by Airshow

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.