Results 1 to 3 of 3
  1. #1
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default Validating a string, containing either numbers or letters

    Hi again, I wasn't sure if I should post this question here, or in a different section, but I am still consider myself new to Java, so I posted it here.

    I am working on a JavaScript problem, and need a bit of help. I'll post my code first.

    Java Code:
       /** Creates global month array */
       /** Populate this array of all possible months */
     
       var months = new Array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");
    
       /** Method that displays a message when a voter is valid/invalid */
       function addVoter(form) 
       {
            if(isVoterValid(form)) 
            {   
            	numVoters++;    
            	fn = myform.firstName.value;
            	ln = myform.lastName.value; 
                alert("Voter: " + fn + " " + ln + ", has been entered into the system.");
            }
            else     
           	{  
            	alert("Voter information is invalid, please try again.");  
            }
       }         
    
       /** Method to get the numeric index of the month String that the 
            user has entered. */
       function getMonthIndex(monthStr) 
       {
       		var i; 
       		for (i = 0; i < 12; i++)
       		{
       			if (monthStr = months[i]) //This method converts the monthStr into a integer, whether it is already an int or not.
       			{
       				monthStr = 1 + i;        
       			} 
       		}	
       }
       
       /** Method to verify whether all information for this user is valid. */
       function isVoterValid(form) 
       {
       		firstName = myform.firstName.value;
       		
       		if (firstName == '')
       		{
       			return false;
       		}
       		
       		lastName = myform.lastName.value;
       		
       		if (lastName == '')
       		{
       			return false;
       		}
       		
       		ssn = myform.ssn.value;
       		
       		if (ssn == '')  
       		{
       			return false;
       		}
       		
       		bmonth = myform.bMonth.value;
       		getMonthIndex(bmonth);  
       		
       		if (bmonth > 12 || bmonth < 1)
       		{  
       			return false;
       		}
       		
       		bday = myform.bDay.value;
       		 
       		if (bday > 31)
       		{
       			return false;
       		}
       		
       		byear = myform.bYear.value;
       		
       		if (byear < 1890) //I set the max age a voter can be as 120.  
       		{
       			return false;
       		} 
       		 
       		dyear = myform.dYear.value;
       		 
       		if (dyear < byear)
       		{
       			return false; 
       		}
       	    
       		dmonth = myform.dMonth.value;
       		getMonthIndex(dmonth);
       		
       		if (dmonth > 12 || dmonth < 1)
       		{
       			return false;
       		}
       		
       		dday = myform.dDay.value;
       		
       		if (dday > 31)
       		{
       			return false;
       		}
       		
       		if (dyear == byear && dmonth < bmonth)
       		{
       			return false;
       		}
       		  
       		if (dyear == byear && dmonth == bmonth && dday < bday)
       		{
       			return false;       
       		}
       	   
          return true; //If none of these 'if' statements are executed, then we know we have a valid date.
       }   
     
       /** This method clears all fields */
       function resetValues(form) 
       {
          form.firstName.value = '';
          form.lastName.value = '';
          form.ssn.value = '';
          form.bMonth.value = '';
          form.bDay.value = '';
          form.bYear.value = '';
          form.dMonth.value = '';
          form.dDay.value = '';
          form.dYear.value = '';
          
          // reset number of voters added to 0 (comment out if don't want to clear this)
          numVoters = 0;
          document.myform.numVotersField.value = numVoters;
       }
    This is where my problem is. There is some more code, that creates the page where the information is entered, and where the methods resetValues() and addVoter() are called, but that works perfectly.

    What I am supposed to do is validate the voter information that is inputed. I have succesfully created all the validation statements needed, except one. I do not know how to validate the month if it is entered as a month string, for example, "january". I am supposed to call the method getMonthIndex() but don't know what to do there. As you know, I can't change a string value to an int, so my getMonthIndex() doesn't work. My code validates everything correctly except month names.

    Thanks for any help you guys can give, this little prob has me tied in a knot.

    -Mace
    When in doubt, use a lightsaber

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Validating a string, containing either numbers or letters

    This is a Java forum, not a Javascript forum (hence the name "Java-Forums.org"), and I'm sure that you know that these two languages are about as similar as ham is to hamster. I suggest Googling for a Javascript forum and asking this question there.

  3. #3
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default Re: Validating a string, containing either numbers or letters

    Sorry, my mistake:)

    Okay, will do!
    When in doubt, use a lightsaber

Similar Threads

  1. Stop user entering letters in GUI, numbers only
    By africanhacker in forum New To Java
    Replies: 3
    Last Post: 03-11-2011, 01:25 PM
  2. counting letters in a string
    By beandip408 in forum New To Java
    Replies: 12
    Last Post: 09-29-2010, 01:44 PM
  3. converting letters to numbers
    By fenhoff in forum New To Java
    Replies: 2
    Last Post: 01-24-2010, 04:45 AM
  4. Validating for length of String
    By Bascotie in forum New To Java
    Replies: 1
    Last Post: 10-11-2009, 11:45 AM
  5. validating a string for numbers and letters?
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 09:17 AM

Posting Permissions

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