Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If selected, check textarea contents for string

Status
Not open for further replies.

TrinityD

IS-IT--Management
Feb 9, 2013
5
GB
Hi all,

Looking for a little guidance here please. What I'm looking to do is check the value of a select box and if a certain option is selected, then check the value of a textarea for a string from a list.

So for example, using the following form, the logic I need to get working is:

OnSumbit - If cust_type EQ Private then does textarea contain 111,222 or 333?

I know how to get the validation to check if Private is selected and I know how to check for certain strings, I just don't know how to combine both of these to work together.

Code:
<form name="reportit" action="" method="POST">

   <select name="cust_type">
 <option>Contract</option>
 <option>Private</option>
   </select>

   <textarea name="notes">Test 111 Test 
   </textarea>

   <input type="submit" name="goforit" value="Proceed">
 
 </form>

Thanks in advance for your input.
 
As jpade suggests check the first one, and then inside check the second one.

Code:
if(Private is selected)
{
[indent]if(textarea.value==111 or ==222 or ==333)[/indent]
[indent]{[/indent]
[indent]  Do things[/indent]
[indent]}[/indent]
[indent]else[/indent]
[indent]{[/indent]
[indent]  Do things if textarea does not contain said values. [/indent]
[indent]}[/indent]
}

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi,

Thanks for your replies. Sorry, now that I re-read it my original post was pretty unclear about the issue I was having at the time.

I have a working solution now but stuck with one part of it. When the confirm button is cancelled, how do I stop submission of the form? I have tried many different combinations of "return false" but just cannot get it to work. My attempts either end in submission going through anyway or submit not working at all. My background is all server-side languages and JS is causing me so many issues.

Code:
function f1(){
	str=job_notes.value;
	x=customer_type.value;
	if(x=="Private"){
		if(str.indexOf('111') == -1 && str.indexOf('222') == -1 && str.indexOf('333') == -1){
		confirm("Data is missing, continue?");
		}
	}
}

Any suggestions on where I am going wrong?

Thanks in advance.
 
Hard to say without seeing how you are calling your validation function. Technically if you are calling it from a submit button's onclick event or even the form's onsubmit event you would need to return a false to the event in question.

Code:
<form ... onsubmit="[b]return[/b] validationFunction();">

or 

<input type="submit" ... onclick ="[b]return[/b] validationFunction();">

If your validation function returns a false, it will be returned to the event and cancel it.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top