SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot greeneye's Avatar
    Join Date
    Apr 2006
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Name generator won't validate

    Hi there,

    I'm building a name generator where a person puts their name in and gets a different name out. I've set it up so you have to put in something.

    When I test it it tells me I haven't put a name in, but obviuosly I have. I can't see where the code is wrong.

    Here are the relevant bits, I've cut out all the other code....
    Code:
    firstname1 = new Array("Gold"...etc up to 26)
    firstname2 = new Array("Gold"...etc up to 26)
    firstname2 = new Array("Gold"...etc up to 26)
    
    function getSillyName(myForm) {
    	firstNm = myForm.fName.value.toUpperCase()
    
    if (firstNm != "") {
    		firstNum1 = firstNm.charCodeAt(0) - 65
    		firstNum2 = firstNm.charCodeAt((lastNm.length-2)) - 65
    		firstNum3 = firstNm.charCodeAt((lastNm.length-1)) - 65
    
    if (firstNum1 >= 0 && firstNum1 <= 25 && firstNum2 >= 0 && firstNum2 <= 25 && firstNum3 >= 0 && firstNum3 <= 25) {
    
    sillyName = firstname1[firstNum1] + " " + firstname2[firstNum2]
    				sillyRanking = firstname3[firstNum3]
    
    
    alert("Your silly name is " + sillyName 
    					+ "Your Ranking is " + sillyRanking)
    					return false
    					
    					}
    				}
    alert("That's not a valid first name")
    				myForm.fName.focus()
    				myForm.fName.select()
    				return false
    			}
    My form is as follows:

    HTML Code:
    <form onsubmit="return getSillyName(this)" action"#">
    <p>First Name
    <input name="fName" type="text" size="30" /></p>
    <p><input type="submit" value="Submit" />&nbsp;<input type="reset" /></p>
    </form>
    Thanks for your help

    greeneye
    Last edited by greeneye; Jul 6, 2006 at 10:22.
    Hello

  2. #2
    SitePoint Zealot greeneye's Avatar
    Join Date
    Apr 2006
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please someone...anyone...

    Could really use some help here :slightly desperate look in the eyes:
    Hello

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There's no ';' at the end of every line! And there's no 'else' for the first 'if'! Here, try this:
    Code:
    function getSillyName(myForm) {
    	firstNm = myForm.fName.value.toUpperCase();
    
    if (firstNm != "") {
    		firstNum1 = firstNm.charCodeAt(0) - 65;
    		firstNum2 = firstNm.charCodeAt((firstNm.length-2)) - 65;
    		firstNum3 = firstNm.charCodeAt((firstNm.length-1)) - 65;
    
    if (firstNum1 >= 0 && firstNum1 <= 25 && firstNum2 >= 0 && firstNum2 <= 25 && firstNum3 >= 0 && firstNum3 <= 25) {
    
    sillyName = firstname1[firstNum1] + " " + firstname2[firstNum2];
    				sillyRanking = firstname3[firstNum3];
    
    
    alert("Your silly name is " + sillyName 
    					+ "Your Ranking is " + sillyRanking);
    					return false;
    					
    					}
    				}else alert("That's not a valid first name");
    				myForm.fName.focus();
    				myForm.fName.select();
    				return false;
    			}
    Hope this helps, so you can go to sleep already
    Saul

  4. #4
    SitePoint Zealot greeneye's Avatar
    Join Date
    Apr 2006
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you php_daemon I really appreciate your help!

    Unfortunately, its still not working....

    Here is my amended code.

    Code:
    <script language="javascript">
    
    firstname1 = new Array("Gold"....+25 more...);
    firstname2 = new Array("Gold"....+25 more...);
    firstname3 = new Array("Gold"....+25 more...);
    
    function getPirateName(myForm) {
    	firstNm = myForm.fName.value.toUpperCase();
    
    if (firstNm != "") {
    		firstNum1 = firstNm.charCodeAt(0) - 65;
    		firstNum2 = firstNm.charCodeAt((lastNm.length-2)) - 65;
    		firstNum3 = firstNm.charCodeAt((lastNm.length-1)) - 65;
    
    if (firstNum1 >= 0 && firstNum1 <= 25 && firstNum2 >= 0 && firstNum2 <= 25 && firstNum3 >= 0 && firstNum3 <= 25) {
    
    pirateName = firstname1[firstNum1] + " " + firstname2[firstNum2];
    				pirateRanking = firstname3[firstNum3];
    
    alert("Your pirate name is " + pirateName 
    					+ "Your Ranking is " + pirateRanking);
    					return false;
    					
    					}
    				}else alert("That's not a valid first name");
    				myForm.fName.focus();
    				myForm.fName.select();
    				return false;
    			}
    
    </script>

    I am still trying to work it out myself however being a real beginner it is very difficult for me to work it out.

    I am using the Visual Quickstart book as a reference and I don't think it is that good as when I adapt their scripts they never seem to work....

    Any help will be massively appreciated!
    Hello

  5. #5
    SitePoint Zealot greeneye's Avatar
    Join Date
    Apr 2006
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Am I asking a hard question that noone can answer? A boring question that noone can be bothered to answer? Or an easy question that everyone is too insulted to answer?

    Come on guys...throw me a bone please! I'll return the favour if I can
    Hello

  6. #6
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, for one thing, you haven't provided any exact error messages. That means I'll have to copy/paste your code and debug it myself. If you had a Javascript console error listed, one of us might go 'Aha! I've seen that before!'.

    I'm examining it right now. Can you post the form that your using to submit the name?

  7. #7
    SitePoint Zealot greeneye's Avatar
    Join Date
    Apr 2006
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The form is here :

    HTML Code:
    <form onsubmit="return getSillyName(this)" action"#"> <p>First Name <input name="fName" type="text" size="30" /></p> <p><input type="submit" value="Submit" />&nbsp;<input type="reset" /></p> </form>
    I have have dramatically cut down the javascript code to make it easier to look at. I could upload it onto a spare domain I have and then maybe you could check it that way. Would that work or is this enough.

    Thank you very much Darkwater!
    Hello

  8. #8
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can zip up the relevant files and attach them to your post. It shouldn't be that big a file.

  9. #9
    SitePoint Zealot greeneye's Avatar
    Join Date
    Apr 2006
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hope that worked.....

    edit: ok that didn't work....sorry give me a minute...
    Hello

  10. #10
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've made some changes to your code. It works in IE and FF. I added code comments to the Javascript and HTML to indicate what changes I made.

    P.S. I want the 3 arrays you have. I want to know what my pirate name will be!

    Code:
    <script language="javascript">
    // I had to add my own to get this to work.
    var firstname1 = new Array("Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold");
    var firstname2 = new Array("Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold");
    var firstname3 = new Array("Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold","Gold");
    
    // No need to pass form into function
    function getPirateName() {
    
    	// Use 'var' to declare variables. Not required, but good programming.
    	// Use 'document.'. Not required all the time, but good programming.
    	var firstNm = document.myForm.fName.value.toUpperCase();
    
    	if (firstNm != "") {
    			var firstNum1 = firstNm.charCodeAt(0) - 65;
    			// Had a reference to lastNm here so code didn't work.
    			var firstNum2 = firstNm.charCodeAt((firstNm.length-2)) - 65;
    			// Had a reference to lastNm here so code didn't work.
    			var firstNum3 = firstNm.charCodeAt((firstNm.length-1)) - 65;
    			
    			// Removed '<= 25'. It seemed confusing to me to have equal signs on each side of logic.
          if (firstNum1 >= 0 && firstNum1 < 26 && firstNum2 >= 0 && firstNum2 < 26 && firstNum3 >= 0 && firstNum3 < 25) {
    
    				pirateName = firstname1[firstNum1] + " " + firstname2[firstNum2];
    				pirateRanking = firstname3[firstNum3];
    
    				alert("Your pirate name is: " + pirateName + "\nYour Ranking is: " + pirateRanking);
    					
    			}
    		}else{
    				alert("That's not a valid first name");
    				// Added 'document.'
    				document.myForm.fName.focus();
    				document.myForm.fName.select();
    		}
    }
    // Several braces were missing from above. Make sure you indent and cleanly format code. I usually open and close braces before I
    // add code to them.
    </script>
    </head>
    
    <body>
    <!-- For the Javascript function, it is not necessary to submit the form or pass the form as an object to the function. -->
    <!-- Note that there is no action now and the input at the bottom is of type 'button' and not 'submit'. -->
    <!-- You could even skip the form all together by giving the input field an id, like id='fName', and using -->
    <!-- document.getElementById("fName").value instead. -->
    <form name="myForm" method="post" action="">
      <p>
        <input type="text" name="fName">
    </p>
      <p>
        <input type="button" name="button" value="Get Your Pirate Name!" onClick="getPirateName();">
    </p>
    </form>

  11. #11
    SitePoint Zealot greeneye's Avatar
    Join Date
    Apr 2006
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much!

    the arrays are as follows. The whole will eventually be much more complex though. I want to do like pirate trump cards with loads of different stats!

    DELETED as will be changed and improved with time!

    Thanks again! I'd be interested to know what your pirate name is!
    Last edited by greeneye; Jul 7, 2006 at 07:18.
    Hello

  12. #12
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My pirate name in the Princess Bride movie:

    Princess: I know who you are! Your cruelty reveals everything! You are Pirate Prince Black Legged Jack! Admit it!

    Jack: With pride! What can I do for you?

    Princess: You can die slowly, cut into a thousand pieces!

    Jack: tsk tsk tsk. Hardly complimentary, highness. Why loose your venom on me?

    Princess: You killed my love...

    Jack: It's possible. I kill a lot of people.


    I'm such a nerd...

  13. #13
    SitePoint Zealot greeneye's Avatar
    Join Date
    Apr 2006
    Location
    London
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent!

    Well mine is Blue-Legged Lenny
    and my rank is Serving Wench...typical!
    Hello


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
  •