SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2004
    Posts
    419
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Email validation issue

    Hi,

    I have a simple bit of HTML / Javascript to validate email addresses entered into a field, split by a comma.

    Code HTML4Strict:
    <html>
    <body>
    <form name="myForm" onsubmit="return chkEmail()" method="post">
    e: <input type="text" name="fname">
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>

    Code JavaScript:
    function validateEmail(email) { 
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        return re.test(email);
    }
     
    function chkEmail(){
    	var myString = document.forms["myForm"]["fname"].value;
    	var mySplitResult = myString.split(",");
    	for(i = 0; i < mySplitResult.length; i++){
    		emailCheck = validateEmail(mySplitResult[i]);
    		alert(mySplitResult[i] + " - " + emailCheck);
    		if (emailCheck = 'false') {
    			alert(emailCheck);
    		}
    	}
    }

    If I enter e.g. "you@me.com,this@that.com" into the form, the alert debugs show:

    First: you@me.com - true
    Second: false
    Third: this@that.com - true
    Fourth: false

    The first debug seems to show that the condition is true, but then the if statement to check for a "false" value is firing, showing the value of "emailCheck" = "false", even though on the preceding alert, the value = "true".

    I'm not sure what's going wrong.

    Apologies for any silly mistakes.

    Thanks

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Here you are assigning the value of 'false' to the variable emailCheck
    Code:
    if (emailCheck = 'false') {
    Since the assignment successfully occurred, the condition evaluates to true.

    If you want to compare them, you should use === instead.

    Code javascript:
    if (emailCheck === 'false') {
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •