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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    School Project - Validating Forms -

    Hello, I have a school project that involves validating forms. It is the end of the semester project, and I have taken it home with me on a flash drive . The project involves validating a form entry to check to see if it has numbers or letters or only numbers or only letters etc.

    Here is my javascript so far:

    Code:
    <SCRIPT Language="JavaScript">
    var count1;
    var count2;
    var count3;
    function bc()
    {
    	for(count1=0;count1<=9;count1++)
    		{
    			if(document.ab.name.value.indexOf(count1)==-1)
    			{
    			
    			}
    			else
    			{
    			alert('You can only have letters in the name.');
    			return;
    			}
    		}
    	if(document.ab.name.value=="")
    		{
    			alert('Please enter a name.');
    			return;
    		}
    
    	if(document.ab.add.value=="")
    		{
    			alert('Please enter an address.');
    			return;
    		}
    	for(count3=0;count3<=9;count3++)
    		{
    			if(document.ab.add.value.indexOf(count3)==-1)
    			{
    				if(count3==9)
    				{
    					alert('You must have a number in your address.');
    					return;
    				}
    			}
    			else
    			{
    				break;
    			}
    		}
    	for(count2=0;count2<=9;count2++)
    		{
    	if(document.ab.city.value=="")
    		{
    			alert('Please enter a city.');
    			return;
    		}
    			if(document.ab.city.value.indexOf(count2)==-1)
    			{
    			
    			}
    			else
    			{
    			alert('You can only have letters in the city.');
    			return;
    			}
    		}
    	if(document.ab.state.value=="")
    		{
    			alert('Please enter a state.');
    			return;
    		}
    
    	if(document.ab.state.value.length==2)
    		{
    		}
    	else
    		{
    		alert('You must have two letters in the state.');
    		return;
    		}
    	if(document.ab.zip.value=="")
    		{
    			alert('Please enter a zip code.');
    			return;
    		}
    
    	if(document.ab.email.value.indexOf('@')==-1)
    		{
    		alert('Please enter a valid email.');
    		return;
    		}
    	if(document.ab.email.value.indexOf('.')==-1)
    		{
    		alert('Please enter a valid email.');
    		return;
    		}
    	if(document.ab.pw.value=="")
    		{
    			alert('Please enter a password.');
    			return;
    		}
    document.ab.submit();
    }
    </SCRIPT>
    I must make sure that the .name field only contains letters. I have it checking for numbers..but how would I make it check to make sure it has no signs..such as " # " or " ! " or something.

    and

    I have to make sure the address ".add" has numbers and letters. I have it so that it checks for numbers already, but how would I make sure it also has letters?

    Thanks RockyDr
    Last edited by RockyDr; 12-01-2006 at 07:28 PM.

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The best way to check for letters in the name is to check exactly that. Not check for what shouldn't be in there.

    Did your teacher show you regular expressions?

    If not, you could loop through each character in the name to see if it is a letter.

    Code:
    var s = document.ab.name.value;
    
    for (var i = 0; i < s.length; i++) {
      if (('a' <= s[i] && s[i] <= 'z') || ('A' <= s[i] && s[i] <= 'Z')) {
        // is a letter
      } else {
        // is not a letter
      }
    }
    The first half of the if statement checks to see if it is a lowercase letter and the second half checks to see if it is upper.

    You could check other conditions just by changing the if expression.

    david_kw

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply..yet that confused me sooooo much

    I'm not sure I understand most of the code you put up there..but I will try to go through and learn each part.

    Code:
    var s = document.ab.name.value;
    Shouldn't it be == instead? or would that be different?

    Code:
    for (var i = 0; i < s.length; i++)
    For this part why do you use numbers to check for letters?

    Code:
    if (('a' <= s[i] && s[i] <= 'z') || ('A' <= s[i] && s[i] <= 'Z'))
    This is the part that really made my brain explode...so for me to try to understand it..i'm gonna break it into sections. the first part

    ('a'<=s[i] && s[i] <= 'z')

    Does that mean it checks from lowercase a all the way to z?
    Or does it just check a and z?
    Does increasing the i to 1 or 2 make it go up a letter..like b and y or c and x?
    One last question.. i don't understand the || sign..what is that for?

    Code:
     // is a letter
    Does this mean I put the code here that I want the run if it evaluates to having a letter?
    Last edited by RockyDr; 12-01-2006 at 08:41 PM.

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whoops. I guess since I gave you part of the answer I better explain it.

    Code:
    var s = document.ab.name.value;
    Shouldn't it be == instead? or would that be different?

    == test to see if two things are the same
    = makes s have the same value as document.ab.name.value
    so the = is what you want in this case


    Code:
    for (var i = 0; i < s.length; i++)
    For this part why do you use numbers to check for letters?

    you aren't using the numbers to test for letters. you are using the numbers to check each character in the string to see if it is what you want it to be
    For example:
    s = "david";
    Then:
    s[0] = 'd';
    s[1] = 'a';
    s[2] = 'v';
    etc

    So basically you are going through all the typed characters and checking each one.


    Code:
    if (('a' <= s[i] && s[i] <= 'z') || ('A' <= s[i] && s[i] <= 'Z'))
    This is the part that really made my brain explode...so for me to try to understand it..i'm gonna break it into sections. the first part

    ('a'<=s[i] && s[i] <= 'z')

    Does that mean it checks from lowercase a all the way to z?
    Or does it just check a and z?

    It checks all the letters from a to z.
    Picture a list of letters
    abcdefghijklmnopqrstuvwxyz
    This line says if the letter you want to know about is bigger in the list than 'a' and smaller in the list than 'z' it is true
    && means and

    Does increasing the i to 1 or 2 make it go up a letter..like b and y or c and x?

    i is the letter you are interested in that someone typed. So when i = 0 then s[i] = 'd'
    i = 1 then s[i] = 'a'
    So, again, you are checking each character typed to see if it is a letter.

    One last question.. i don't understand the || sign..what is that for?

    || is OR

    which means it is
    if ((typed letter is bigger than 'a') and (typed letter is lower than 'z') or (typed letter is bigger than 'A') and (typed letter is lower than 'Z')) then


    Code:
     // is a letter
    Does this mean I put the code here that I want the run if it evaluates to having a letter?

    Yep. That is a comment in javascript. Anything written on the line after // is ignored by the browser.


    No offense but either you missed too many classes or your teacher is giving you an assignment that you aren't prepared to do.

    david_kw

  • #5
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you soo much!

    You should be a Web Design Teacher


    Umm..about my teacher...

    Hes..an okay teacher, but he spends more time talking about Warcraft then explaining the assignments...

    He explains most of the stuff..yet he doesn't explain the &&'s or the ||'s or how to check for letters..we've never had any assignments on them..

  • #6
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay now I have a problem again

    I tried using the bit of code you put up there..and well when I click submit it says "You can only have letters in the name." even when theres only letters..

    Code:
    var s = document.ab.name.value;
    
    	for(var count1=0;count1<s.length;count1++)
    		{
    			if(('a'<=s[count1] && s[count1]<='z')||('A'<=s[count1] && s[count1]<='Z'))
    			{
    			
    			}
    			else
    			{
    			alert('You can only have letters in the name.');
    			return;
    			}
    		}

  • #7
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It works for me. Are you sure there are only letters? No spaces or anything?

    david_kw

  • #8
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nope all I had was a single letter in there..

    I tried it on just a blank new document..it still does the same thing = (

    heres the code:

    Code:
    <HTML>
    <HEAD>
    <SCRIPT Language="JavaScript">
    function bc()
    {
    var s = document.ab.name.value;
    	for(var count1=0;count1<s.length;count1++)
    		{
    			if(('a'<=s[count1] && s[count1]<='z')||('A'<=s[count1] && s[count1]<='Z'))
    			{
    			
    			}
    			else
    			{
    			alert('You can only have letters in the name.');
    			return;
    			}
    		}
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <form name="ab">
    <input type="text" name="name">
    <input type="button" onClick="bc();">
    </form>
    </BODY>
    </HTML>

  • #9
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    this is response to your teachers teaching methods, lol. if you want to learn all that stuff, you should take a look at http://www.w3schools.com .
    they have all the basics that you should know. I still use it myself from time to time because i forget things after awhile of not using it.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #10
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Replace this line:
    Code:
    if(('a'<=s[count1] && s[count1]<='z')||('A'<=s[count1] && s[count1]<='Z'))
    with this:
    Code:
    if(('a' >= s[count1] && s[count1] <= 'z') || ('A' >= s[count1] && s[count1] <= 'Z'))
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #11
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried what you said Dumpfi..
    It still says that I must only have letters even when only a is only entered..

  • #12
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm, I'm honestly not sure why that didn't work. It was working in FF but not IE. I've had it working before. But try change:

    s[count1]

    to

    s.charAt(count1)

    like this

    Code:
    <HTML>
    <HEAD>
    <SCRIPT Language="JavaScript">
    function bc()
    {
    var s = document.ab.name.value;
    alert(s + ' is typeof ' + typeof(s));
      for(var count1=0;count1<s.length;count1++)
        {
          if(('a'<=s.charAt(count1) && s.charAt(count1) <='z')||('A'<=s.charAt(count1) && s.charAt(count1)<='Z'))
          {
          
          }
          else
          {
          alert("You can only have letters in the name. it is = '" + s.charAt(count1) + "' at " + count1);
          return;
          }
        }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <form name="ab">
    <input type="text" name="name">
    <input type="button" onClick="bc();">
    </form>
    </BODY>
    </HTML>
    that worked for me in both. Maybe IE has some problem addressing a string as an array? I don't recall that problem in IE6 but I recently upgraded to IE7. I'll have to do some testing to see what's up.

    david_kw

  • #13
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So now I can't get it to work in IE6 or 7. Maybe I'm imagining that I ever had that working in IE. Anyway, forget the s[i] stuff and stick to s.charAt(i) and you should be ok it looks like.

    Also note I edited your alert and added a new one. Be sure to fix those.

    david_kw
    Last edited by david_kw; 12-03-2006 at 06:22 AM.

  • #14
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    AWESOME! Thanks for helping me get this resolved!

    Now i've run into the next problem...

    I'm trying to check that the address has both numbers and letters...and well I know screwed up somewhere..but I just can't figure it out...My code is soo screwed up..that I know it would only give you guys headaches if I posted it..

    So heres my question..

    How do you check that .add has both letters and numbers and nothing else (signs)?
    Last edited by RockyDr; 12-03-2006 at 08:05 AM.

  • #15
    New Coder
    Join Date
    Dec 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the code im using to try to make sure that .add only had numbers and letters...

    By the way... x is equal to .add

    Code:
    	for(count2=0;count2<=x.length;count2++)
    		{
    			if((0<=x.charAt(count2) && x.charAt(count2)<=9)||('a'<=x.charAt(count2) && x.charAt(count2)<='z')||('A'<=x.charAt(count2) && x.charAt(count2)<='Z'))
    			{
    			
    			}
    			else
    			{
    				alert('You must only have numbers and letters in your address.');
    				return;
    			}
    		}
    	for(count10=0;count10<=x.length;count10++)
    		{
    			if(('a'<=x.charAt(count10) && x.charAt(count10)<='z')||('A'<=x.charAt(count10) && x.charAt(count10)<='Z'))
    			{
    			break;
    			}
    			else
    			{
    				if(count10==x.length)
    				{
    					alert('You must have letters in your address.');
    					return;
    				}
    			}
    		}
    	for(count11=0;count11<=x.length;count11++)
    		{
    			if('0'<=x.charAt(count11) && x.charAt(count11)<='9')
    			{
    			break;
    			}
    			else
    			{
    				if(count11==x.length)
    				{
    					alert('You must have numbers in your address.');
    					return;
    				}
    			}
    		}
    I don't understand why this doesn't work


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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