Jump to content

Checking a substring for a letter


sunflower

Recommended Posts

well I'm back. :) I seem to be going further in over my head with my name generator script. I'm trying to tell it that if 3 conditions are present on the form to give me random names from a certain array. The fist two conditions were easy.. It's the third condition that I'm having trouble writing. For the third condition I want it to check the first letter of the name in the text input box of the form, see what it is, and then if its an A name, for instance, I will have it to choose names from an array with all A names. This is what i have so far thats not working.. and i know the problems in the third condition cause it works if i take it out..

var firstname = nameform.firstname.value;if((nameform.gender.options[0].selected == true) && (nameform.species.options[0].selected == true)&& (firstname.substring(0) == 'A')){i = Math.floor(Math.random() * malebothanA.length);nameform.output.value=malebothanA[i] + " " + bothanlastname[r]}

I wouldnt be surprised if I wrote that all wrong. I just hope someone here can help me cause I dont think I could ever find any js tutorials that explain how to do this in a way that a newbie like me can understand.

Link to comment
Share on other sites

Thankyou Deirdre's dad. I have been reading about charAT. I feel like the problem is in the way i am referencing the form field. I have tried it this way,

if((nameform.gender.options[0].selected == true) && (nameform.species.options[0].selected == true)&& (nameform.firstname.value.charAt(0) == 'A'));

I have also tried with the word document in front like this, document.nameform.firstname.value.charAT. Neither one is working.

Link to comment
Share on other sites

First off, you should ditch the dot notation and assign id's to your elements and use document.getElementById()Second, Try alerting the value of your text box, ie:alert(document.getElementById("firstName").value); (EDIT: This is assuming you put an id='firstName' on the appropriate text box)if (....) {...Is it what you expect it to be?

Link to comment
Share on other sites

To clarify. Shadow is suggesting a form that is structured kind of like this:

<form action="" method="post">   <select id="gender">	  <option value="blah">blah</option>	  <option value="blah">blah</option>	  <option value="blah">blah</option>   </select>   <select id="species">	  <option value="blah">blah</option>	  <option value="blah">blah</option>	  <option value="blah">blah</option>   </select>   <input type="text" id="firstName"></form>

Now you should reliably get data from references like these:

document.getElementById("gender").options[0].selecteddocument.getElementById("species").options[0].selecteddocument.getElementById("firstName").value

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...