Is something wrong with getElementById

RE doesn't work when used with getElementById

Page 1 of 1

5 Replies - 1494 Views - Last Post: 10 June 2010 - 05:47 AM

#1 singularity  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 184
  • Joined: 17-October 08

Is something wrong with getElementById

Posted 01 June 2010 - 04:18 AM

Here is my simple RE to validate an email, It doesn't work like this
if(document.getElementById("email").match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/))
    {
       alert("good Email");
}


However If I use :
if(document.forms[0].elements[0].match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/))
{
alert("Good Email");
}


Then it works, does anyone have an idea what is wrong with the first snippet.


fyi html code is below:
<form name = "example" id = "example" action = "#" method="post">
<table align="center" border="1">
<tr><td>Email</td><td><input type="text" value="" name="email" id="email"></td></tr>
<tr><td>Phone</td><td><input type="text" value="" name="phone" id="phone"></td></tr>
<tr><td>Select Gender</td><td><input type="radio" name="radio1" id="radio1">Male<input type="radio" 

name="radio1" id="radio1">Female</td></tr>
<tr><td></td><td><input type="button" value="Register" onclick = "return valid()"></td></tr>
</table>
</form>

This post has been edited by singularity: 01 June 2010 - 04:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Is something wrong with getElementById

#2 JMRKER  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 130
  • View blog
  • Posts: 837
  • Joined: 25-October 08

Re: Is something wrong with getElementById

Posted 01 June 2010 - 05:26 AM

Try...
if(document.getElementById("email").value.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/)) 
    { 
       alert("good Email"); 
}


:bananaman:
Was This Post Helpful? 1
  • +
  • -

#3 singularity  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 184
  • Joined: 17-October 08

Re: Is something wrong with getElementById

Posted 02 June 2010 - 03:13 AM

View PostJMRKER, on 01 June 2010 - 11:26 AM, said:

Try...
if(document.getElementById("email").value.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/)) 
    { 
       alert("good Email"); 
}


:bananaman:


Thanks,
I modified it like this:
if(!(document.getElementById("email").value.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-

]+)+/))) 
    { 
       alert("Please enter a valid email"); 
       document.getElementById("email").focus();
       return false;
}


but it is validating also text@text I mean it doen't bother for dot and after dot letters
Was This Post Helpful? 0
  • +
  • -

#4 JMRKER  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 130
  • View blog
  • Posts: 837
  • Joined: 25-October 08

Re: Is something wrong with getElementById

Posted 02 June 2010 - 07:04 AM

Try this ...
if (!(document.getElementById("email").value.match(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/))) { 
       alert("Please enter a valid email"); 
       document.getElementById("email").focus();
       return false;
}


Was This Post Helpful? 0
  • +
  • -

#5 singularity  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 184
  • Joined: 17-October 08

Re: Is something wrong with getElementById

Posted 10 June 2010 - 05:05 AM

View PostJMRKER, on 02 June 2010 - 01:04 PM, said:

Try this ...
if (!(document.getElementById("email").value.match(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/))) { 
       alert("Please enter a valid email"); 
       document.getElementById("email").focus();
       return false;
}




Sorry for the late reply. I modified it accordingly, but still it is not happening

if(!(document.getElementById("email").value.match(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/)))   
    {
      alert("Valid email Id is Not filled out");
       return false;   
      }
   return true;

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Is something wrong with getElementById

Posted 10 June 2010 - 05:47 AM

I recommend using the RegExp.test() method. that will return true/false. the String.match() only returns null, if there is an error (thus your condition is always true, even if there’s no match)

if (!(/^([^\W_])+([\.\w-])*@([\w-])+(.[\w-]+)+/.test(document.getElementById("email").value))) {
    alert("Valid email Id is Not filled out");
}

This post has been edited by Dormilich: 10 June 2010 - 05:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1