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?

Recommended Answers

All 3 Replies

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

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

...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>

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.