www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Simple app code question for validating

  1. #1
    Join Date
    Apr 2012
    Posts
    47

    Question Simple app code question for validating

    Here is the app I'm working on:
    http://www.grabilla.com/02409-38f8d0...e9d9783b1f.png

    I just have to validate the email address entered. To keep this simple, assume that a valid email address is one that contains an @ sign and a dot(period) with the dot after the @ sign. If the address is invalid, the application should display message like this, depending on what the problem is:

    A valid email address must include an @ sign.

    A valid email address must include a period after the @ sign.

    Here is my JavaScript:
    Code:
    var $ = function (id) {
        return document.getElementById(id); 
    }
    
    var email_click = function () {
        var emailAddress = $("email").value;
        alert ( emailAddress );
    }
    
    var date_click = function () {
        var dateEntry = $("date").value;
    	alert ( dateEntry );
    }
    
    window.onload = function () {
        $("processEmail").onclick = email_click;
    	$("processDate").onclick = date_click;
    }
    Very new to this so any help is much appreciated.

  2. #2
    Join Date
    Apr 2012
    Posts
    55
    You can use indexOf to find the location of a character in a string. It will return -1 if that character does not exist in the string.

    Code:
    var email_click = function () {
        var emailAddress = $("email").value;
        // Find the location of the @ symbol
        var atPos = emailAddress.indexOf('@');
        // Find the location of the dot, if there is one after the @ symbol
        var dotPos = emailAddress.indexOf('.', atPos);
        if(atPos == -1 || dotPos == -1) {
            alert('Invalid email address');
        } else {
            alert('Valid email address');
        }
    }

  3. #3
    Join Date
    Apr 2012
    Posts
    47
    Ok. I plugged that code in, after the var emailAddress line. I then entered my personal valid email address and it brings up the alert for an invalid address still. ??

  4. #4
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    I would recomend using a regular expression.
    This should work:
    HTML Code:
    var email = get.documentById(id).value;
    if(email.match(/@+[a-zA-Z_]+\.[a-zA-Z_]/)){
    //Is valid, do something
    }else{
    //is NOT valid.  Let them know.
    }
    Also, PLEASE validate any emails server side as well.

  5. #5
    Join Date
    Apr 2012
    Posts
    47
    Ok. Would I replace this code:

    Code:
    var email_click = function () {
        var emailAddress = $("email").value;
        alert ( emailAddress );
    }
    ..with that expression and then just enter my alert statements? Sorry, very new to this. I should also mention that this is just for testing through Aptana. Not sure if that matters or not.

  6. #6
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    Not replace, just edit. From:
    Code:
    var email_click = function () {
        var emailAddress = $("email").value;
        alert ( emailAddress );
    }
    :
    to:
    Code:
    var email_click = function () {
        var emailAddress = $("email").value;
    if(emailAddress.match(/@+[0-9a-zA-Z_-]+\.[a-zA-Z_]/)){
    alert('Email Address is valid!');
    }else{
    alert('Email Address is NOT valid!');
    }
    }
    Last edited by NoEffinWay; 04-09-2012 at 01:15 PM. Reason: Error in the RegEx

  7. #7
    Join Date
    Apr 2012
    Posts
    47
    Ok. It now looks like this:

    Code:
    var email_click = function () {
        var emailAddress = $("email").value;
        if(emailAddress.match(/@+[a-zA-Z_]+\.[a-zA-Z_]/)){
           alert('Email Address is valid!');
        }
        else{
           alert('Email Address is NOT valid!');
        }
    }
    but it still just gives me the 'Email Address in NOT valid' alert when I enter a valid email. ?

  8. #8
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    Could you paste the full page?

  9. #9
    Join Date
    Apr 2012
    Posts
    47
    Sure. Here is the entire code:

    Code:
    var $ = function (id) {
        return document.getElementById(id); 
    }
    
    var email_click = function () {
        var emailAddress = $("email").value;
        if(emailAddress.match(/@+[a-zA-Z_]+\.[a-zA-Z_]/)){
           alert('Email Address is valid!');
        }
        else{
           alert('Email Address is NOT valid!');
        }
    }
    
    var date_click = function () {
        var dateEntry = $("date").value;
    	alert ( dateEntry );
    }
    
    window.onload = function () {
        $("processEmail").onclick = email_click;
    	$("processDate").onclick = date_click;
    }

  10. #10
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    yes, full pages always look like that ))

  11. #11
    Join Date
    Apr 2012
    Posts
    47
    Well..the entire page is just this simple application:
    http://www.grabilla.com/02409-38f8d0...e9d9783b1f.png

    so yes, that is the entire javascript code for the page.

  12. #12
    Join Date
    Apr 2012
    Posts
    47

  13. #13
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    can I have the HTML for that page as well...? I see the image but that doesnt help much.

  14. #14
    Join Date
    Apr 2012
    Posts
    47
    Sure thing. Here ya' go:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<title>Strings and Dates</title>
    	<style type="text/css">@import url("strings_and_dates.css");</style>
    	<script type="text/javascript" src="strings_and_dates.js"></script>
    </head>
    
    <body>
    <div id="content">
        <h1>Strings and Dates</h1>
    	<form id="TestApp">
    		<label for="email">Email Address:</label>
    		<input type="text" id="email" /><br />
    		
    		<label>&nbsp;</label>
    		<input type="button" id="processEmail" value="Process" /><br />
    		
    		<p></p>
    		
    		<label for="date">Date:</label>
    		<input type="text" id="date" /><br />
    			
    		<label>&nbsp;</label>
    		<input type="button" id="processDate" value="Process" /><br />
    	</form>
    </div>
    </body>
    </html>
    Thanks for the help by the way. Much appreciated.

  15. #15
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    ...? Works fine for me. I did it in the same page, to make it easier but here:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<title>Strings and Dates</title>
    	
    	<script type="text/javascript">
    	var $ = function (id) {
    		return document.getElementById(id); 
    	}
    
    	var email_click = function () {
    		var emailAddress = $("email").value;
    		if(emailAddress.match(/@+[a-zA-Z_]+\.[a-zA-Z_]/)){
    		   alert('Email Address is valid!');
    		}
    		else{
    		   alert('Email Address is NOT valid!');
    		}
    	}
    
    	var date_click = function () {
    		var dateEntry = $("date").value;
    		alert ( dateEntry );
    	}
    
    	window.onload = function () {
    		$("processEmail").onclick = email_click;
    		$("processDate").onclick = date_click;
    	}
    	</script>
    </head>
    
    <body>
    <div id="content">
        <h1>Strings and Dates</h1>
    	<form id="TestApp">
    		<label for="email">Email Address:</label>
    		<input type="text" id="email" /><br />
    		
    		<label>&nbsp;</label>
    		<input type="button" id="processEmail" value="Process" /><br />
    		
    		<p></p>
    		
    		<label for="date">Date:</label>
    		<input type="text" id="date" /><br />
    			
    		<label>&nbsp;</label>
    		<input type="button" id="processDate" value="Process" /><br />
    	</form>
    </div>
    </body>
    </html>
    See it here: http://script-x.net/
    Last edited by NoEffinWay; 04-09-2012 at 03:41 PM. Reason: Added link

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles