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
    Pensacola, FL
    Posts
    337
    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
    Pensacola, FL
    Posts
    337
    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 12: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
    Pensacola, FL
    Posts
    337
    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,716
    yes, full pages always look like that ))
    signature under construction

  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
    Pensacola, FL
    Posts
    337
    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
    Pensacola, FL
    Posts
    337
    ...? 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 02: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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.15167 seconds
  • Memory Usage 3,010KB
  • Queries Executed 15 (?)
More Information
Template Usage (37):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (9)bbcode_code
  • (1)bbcode_html
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates