Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with functions calling functions within while loops

    Hello. I am having some trouble with my code for a uni assignment. I need to write an email validator by writing three functions that need to have things meet criteria.

    isValidEmailChar needs to have a character satisfy validChars (in code)

    isValidEmailPart needs a string either before or after the @ to:
    -be longer than 1 character in length
    -not start or end with a period '.'
    -not contain a substring of '..'
    -have each character satisfy isValidEMailChar

    isValidEmail needs test the entire address so that it:
    -only has one @
    -split the string around the @ and each substring needs to meet isValidEmailPart
    -the substring after the @ needs to have at least one period.

    The first function is fine.

    The second function runs into some trouble around the while loop, I'm guessing that a sentinel loop is required but how do I make it step through each character of part and assign that character to isValidEmailChar?

    Do I try and get true and false results for each criteria of the function and then put them into an '||' statement so that if all are true the result is true and if one is false the result is false?

    Some help would be greatly appreciated as I have till Friday 10th Jan 2014 to get this done.


    Here is my code. It's about 150 lines so far.






    Code:
     <html> 
        <head> 
            <title> 
                Email Address Validator Test Harness
            </title> 
          <!--@#0-->
            <script> 
    
    //------------------------------------------------------
    // Functions.
    //------------------------------------------------------
    //test a character to check if it is valid
    	function isValidEmailChar(ch)
    	{   "use strict";
    		var validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.';
    		
    		if(validChars.indexOf(ch)>=0)
    			{    ch = true
    			}
    			else{    ch = false
    				}
    		return ch;
    	}
    
    /*test the string before or after the @ to see if it meets: 
    	-is more than one character in length
    	-satisfies isValidEmailChar
    	-does not start or end with '.'
    	-does not contain a substring of '..'
    	*/
    	function isValidEmailPart(part)
    	{   "use strict";
    		var i =0;;
    		var sentinel = false
    		
    		if(part.length>1)
    			{    part = true;
    			}
    			else{    return false;
    				}
    		
    		while(part != sentinel){
    			 for(i=0;i<=length.part;i++){
    			 isValidEmailChar(part[i]);
    			 i++;
    			 }
    			 }
    			 
    			 
    		return part;
    	}
    
    /*tests the entire email address to see if it meets:
    		-only one '@'
    		-the substrings before and after the @ meet isValidEmailPArt
    		-the substring after the @ has at least one '.'
    		*/
    function isValidEmail(email)
    {   "use strict";
        	
    	email.split('@');
    	if(email.length==2)
    		{    email = true}
    		else{     return false}
        return email;
    }
    
            </script> 
        </head> 
        <body> 
            <h1> 
                Email Address Validator Test Harness
            </h1> 
            <table border="1"> 
                <tr> 
                    <th> 
                        Function Name
                    </th> 
                    <th> 
                        Argument
                    </th> 
                    <th> 
                        Button
                    </th> 
                    <th> 
                        Value Returned
                    </th> 
                </tr> 
                <tr> 
                    <th> 
                            isValidEmailChar()
                    </th> 
                    <td> 
                        <input id="isValidCharArg" type="text" size=32> 
                    </td> 
                    <td> 
                        <input type="button" value="invoke" onclick="
                document.getElementById('isValidCharResult').innerHTML
                = isValidEmailChar(document.getElementById('isValidCharArg').value)
                  " > 
                    </td> 
                    <td id="isValidCharResult" > 
                    false
                    </td> 
                </tr> 
                <tr> 
                    <th> 
                            isValidEmailPart()
                    </th> 
                    <td> 
                        <input id="isValidPartArg" type="text" size=32> 
                    </td> 
                    <td> 
                        <input type="button" value="invoke" onclick="
                document.getElementById('isValidPartResult').innerHTML
                = isValidEmailPart(document.getElementById('isValidPartArg').value)
                  " > 
                    </td> 
                    <td id="isValidPartResult" > 
                    false
                    </td> 
                </tr> 
                <tr> 
                    <th> 
                            isValidEmail()
                    </th> 
                    <td> 
                        <input id="isValidArg" type="text" size=32> 
                    </td> 
                    <td> 
                        <input type="button" value="invoke" onclick="
                document.getElementById('isValidResult').innerHTML
                = isValidEmail(document.getElementById('isValidArg').value)
                  " > 
                    </td> 
                    <td id="isValidResult" > 
                    false
                    </td> 
                </tr> 
          <!--@#1-->
          <!-- The above line assists the lecturer to test your code.  When
          the lecturer tests your code, it will be replaced by code that
          will call your functions with string arguments, and note whether
          the correct result is returned. -->
            </table> 
          <!--@#2-->
        </body> 
     </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by Hazzamataz View Post
    ?

    Do I try and get true and false results for each criteria of the function and then put them into an '||' statement so that if all are true the result is true and if one is false the result is false?
    That is horrible and long-obsolete code. An email address is normally validated by a one-line regex. Or in HTML5 <input type="email"> defines a field for an e-mail address (which will be automatically validated when submitted).

    Create a global variable named var valid = true, and set it to false if any of the criteria fails.
    Your function isValidEmailChar(ch) returns false only if the last character is invalid.
    Or set ch=true before the start of the test.


    It is recommended that you place the opening brace following the function, if, else, for, while, do, switch, and try statements on the same line and not on the following line. Apart from that every Javascript statement should be followed by a semi-colon (;). It is quite possible to disregard this advice, but if you do one day it will rise up and bite you in the undercarriage.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 01-05-2014 at 11:09 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much. I'll implement a global variable today and see if it helps.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,641
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Hazzamataz View Post
    Thank you very much. I'll implement a global variable today and see if it helps.
    Using global variables in JavaScript for any purpose other than library references is considered to be extremely poor practice.

    If you wrap ALL of your code inside a self executing function then there is no need for global variables other than for passing in a reference to a library.

    See http://javascriptexample.net/basics90.php for how the starting point for your JavaScript should look - the comment indicates where you should put ALL your code. Alternatively if you are using a library (eg. jQuery) then your starting point would look liike http://javascriptexample.net/jquery01.php
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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