The validation works but when I type something in the textarea it doesn't submit. I need the form to submit after I type something in the textarea box.
Main Topics
Browse All TopicsI'm trying to validate a textbox when certain letters are entered, For example, if b,c,d,e,g,y, or z are entered in the textbox a message will display - "Tell me why you entered this letter". Then it should focus in a textarea to explain why they entered one of those letters. After they explain why they entered that letter, the form should submit.
If they enter a letter besides b,c,d,e,g,y, or z the form should submit without focusing on the textarea for a comment.
There has to be a better way to write the IF statement. Instead of writing all the IF statements below, how?
This is what I have so far. Please show me how to do this the right way. Thanks
<SCRIPT language="JavaScript">
function TheForm () {
var errors = "";
if (document.submitForm.lette
errors += "\n\tLetter - Tell me why you entered this letter. \n";
}
if (document.submitForm.lette
errors += "\n\tLetter - Tell me why you entered this letter. \n";
}
if (document.submitForm.lette
errors += "\n\tLetter - Tell me why you entered this letter. \n";
}
if (document.submitForm.lette
errors += "\n\tLetter - Tell me why you entered this letter. \n";
}
if (document.submitForm.lette
errors += "\n\tLetter - Tell me why you entered this letter. \n";
}
if (document.submitForm.lette
errors += "\n\tLetter - Tell me why you entered this letter. \n";
}
if (document.submitForm.lette
errors += "\n\tLetter - Tell me why you entered this letter. \n";
}
<!--There has to be a better way to write the IF statement. Instead of writing all these IF statements, how?-->
if (errors != ""){
errors += alert(msg + errors + "\n\n");
return false;
}
return true;
}
</script>
<form method="post" name="submitForm" action="send.asp" onSubmit="return TheForm();">
<input name="letters1" type="text" id="letters">
<textarea name="theComments" cols="80" rows="12"></textarea>
</form>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
ok here,
function TheForm (obj) {
var errors = '';
var badLetters = 'b,c,d,e,g,y,z';
var badLettersArr = badLetters.split(',');
var counter = 0;
for(var i=0; i<badLettersArr.length; i++){
if(obj.letters.value.index
errors += '\n\tLetter ' + badLettersArr[i];
counter++;
}
}
if (obj.theComments.value.len
var msg = 'Tell me why you entered ' + (counter>1?'these letters:':'this letter:') + errors;
alert(msg);
obj.theComments.focus();
return false;
}
return true;
}
The problem now if I add another form element. For example if I add this
if (document.submitForm.name.
errors += "\n\tName - Please Enter your name \n";
}
<input name="name" type="text" id="name">
If I fill out "letter" textbox and then "comments" textarea it will not validate "name" textbox. I've tried everything I know but can't get it to work. This is the only problem everything else works great.
Business Accounts
Answer for Membership
by: thirdPosted on 2007-09-11 at 16:54:53ID: 19873000
try this,
l1/DTD/xht ml1-transi tional.dtd "> 999/xhtml" >
Of(badLett ersArr[i]) !=-1){
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtm
<html xmlns="http://www.w3.org/1
<head>
<script language="javascript">
function TheForm (obj) {
var errors = '';
var badLetters = 'b,c,d,e,g,y,z';
var badLettersArr = badLetters.split(',');
var counter = 0;
for(var i=0; i<badLettersArr.length; i++){
if(obj.letters.value.index
errors += '\n\tLetter ' + badLettersArr[i];
counter++;
}
}
if (errors != ""){
var msg = 'Tell me why you entered ' + (counter>1?'these letters:':'this letter:') + errors;
alert(msg);
obj.theComments.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form method="post" name="submitForm" action="send.asp" onSubmit="return TheForm(this);">
<input name="letters" type="text" id="letters">
<textarea name="theComments" cols="80" rows="12"></textarea>
<input type="submit" value="Submit"/>
</form>
</body>
</html>