SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot shiftless's Avatar
    Join Date
    Mar 2005
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    email validator needs tweaking

    I'm using the following code for an email validator:

    Code:
    function emailCheck(emailStr) {
    
    // checks if the e-mail address is valid
    var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
    alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
    return false;
    }
    // make sure the IP address domain is valid
    var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
    if (IPArray != null) {
    for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
    alert("Your email destination IP address is invalid!");
    return false;
          }
       }
    }
    return true;
    }
    But the following email address get rejected:

    firstname.lastname@gmail.com
    macinaw-brownies@yahoo.com
    momsclub90210@yahoo.com



    and I can't figure out why. How do I tweak my code so that these email address will be seen as valid?

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this regex:
    Code:
    ^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$

  3. #3
    SitePoint Zealot shiftless's Avatar
    Join Date
    Mar 2005
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    broken

    this seems to have broken the whole function. after implementing it, it was possible to submit a blank field and it would be accepted. per haps i did it wrong?

    Code:
    function emailCheck(emailStr) {
    
    // checks if the e-mail address is valid
    var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
    alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
    return false;
    }
    // make sure the IP address domain is valid
    var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
    if (IPArray != null) {
    for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
    alert("Your email destination IP address is invalid!");
    return false;
          }
       }
    }
    return true;
    }
    this is the relevant original (not complete) page code fyi:

    Code:
    function emailCheck(emailStr) {
    
    // checks if the e-mail address is valid
    var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
    alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
    return false;
    }
    // make sure the IP address domain is valid
    var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
    if (IPArray != null) {
    for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
    alert("Your email destination IP address is invalid!");
    return false;
          }
       }
    }
    return true;
    }
    
    function validate_form() {
    
    var message="Please complete the following: \n------------------------------------------------\n";
    var noerrors=message;
    
    if (document.getElementById('template').value == "") {
    	message=message + "Please choose a template for your site! \n";
    }
    if (document.getElementById('clubtype').value == "") {
    	message=message + "Please tell us what type of club you have! \n";
    }
    if (document.signup.company.value == "") {
    	message=message + "Please choose a name for your club! \n";
    }
    if (document.signup.first_name.value == "") {
    	message=message + "Please fill in your first name! \n";
    }
    if (document.signup.last_name.value == "") {
    	message=message + "Please fill in your last name! \n";
    }
    if (document.signup.email.value == "") {
    	message=message + "You must provide us with an email address! \n";
    }
    if (document.signup.phone.value == "") {
    	message=message + "Please fill in your phone number! \n";
    }
    if (document.signup.city.value == "") {
    	message=message + "Please fill in your city of residence! \n";
    }
    if (document.signup.state.value == "") {
    	message=message + "Please fill in your state of residence! \n";
    }
    if (document.signup.country.value == "") {
    	message=message + "Please fill in your country of residence! \n";
    }
    if (document.getElementById('numbers').value == "") {
    	message=message + "Please fill in the number of members you have! \n";
    }
    if (document.getElementById('refer').value == "") {
    	message=message + "Please tell us how you found us! \n";
    }
    if (!document.signup.tac.checked){
    	message=message + "You must agree to the Terms and Conditions before proceeding! \n";
    } 
    if(message==noerrors){
    return true;	
    }
    else{
    alert(message);	
    return false;
    }
    }
    
    function OnSubmitCheck() {
    var one = emailCheck(this.email.value);
    var two = validate_form(); 
    if((one == false) || (two == false)) { 
    return false; 
    }
    return true;
    }
    
    function revealState(selMenu){
      newCo = selMenu.options[selMenu.selectedIndex].value
    
      if (newCo != "US"){
        document.getElementById("state-text").style.display = "block";
        document.getElementById("state-menu").style.display = "none";
        document.getElementById("int-cc-warning").style.display = "block";
      }
      else{
        document.getElementById("state-menu").style.display = "block";
        document.getElementById("state-text").style.display = "none";
        document.getElementById("int-cc-warning").style.display = "none";
      }
    }
    
    </script>
    	</HEAD>
    <body MS_POSITIONING="GridLayout">
    <table class="CustomPageMainTable" cellSpacing="0" cellPadding="0">
    	<tr>
    		<td>
    			<table class="NavTopMainTable" cellSpacing="0" cellPadding="0" border="0">
    				<tr>
    					<td class="NavTopContent"><div id="navtop">
    						<div class="titlefloat"><span class="headertitle">Simply the <strong>best</strong> tool for managing your club <strong>online</strong>!</span></div>
    
    						<a href="index.htm"><img width="218" height="66" border="0" alt="Club Spaces" src="images/logo_cs.gif" /></a></div>
    						<div id="nav">
    						<div class="navfloat">
    						<ul class="navlist">
    							<li><a href="index.htm" class="navlist">Home</a></li>
    							<li><a href="services.htm" class="navlist">Services</a></li>
    							<li><a href="signup.htm" class="navlist">Sign-Up</a></li>
    							<li class="end"><a href="sponsors.htm" class="navlist">Sponsors</a></li>
    						</ul>
    
    						</div>
    						<img width="228" height="27" border="0" src="images/navPattern.gif" alt="nav pattern" /></div></td>
    				</tr>
    			</table>
    		</td>
    	</tr>
    	<tr>
    		<td>
    			<table border="0" cellpadding="0" cellspacing="0">
    						<tr>
    							
    							<td class="CustomPageContent">
    								<div id="container">
    									<div class="signup">
    
    										<form name="signup" method="post" action="http://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" onsubmit="return (emailCheck(this.email.value) && validate_form()); return false">
    										<input type="hidden" value="00D500000006qei" name="oid" /> 
    										<input type="hidden" value="http://www.clubspaces.com/welcome.htm" name="retURL" />
    										<span class="page-title">Sign Up</span>
    										<table width="500" cellspacing="0" cellpadding="5" border="0" class="signupTable">
    										<tbody>
    											<tr valign="top">
    												<td><label for="clubtype">Template Selection:</label></td>
                    					                <td><select name="00N50000001I6Xb" title="Template Selected" id="template">

  4. #4
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I've been out of town. The Regex I showed is without any extra delimiters, so you'll need to make it literal or as new Regexp(); in order to use it.

    If you don't want to allow blanks, then I would do a check for a blank value as well.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use this on my forms:

    var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

    function validaemail(){
    var returnval=emailfilter.test(formname.fieldname.value)
    if (returnval==false ){
    alert("Your error message.")

    formname.fieldname.focus();

    return false;
    }


    return true;
    }

    Call it from your form like this:
    <input name="email_contact" type=text class="cn" value="e-mail" size=25 maxlength=50 onChange="validaemail();" >


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •