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!

webpage script for mandatory text box

Status
Not open for further replies.

kat63

Technical User
Jun 7, 2013
1
US
i need help get a script for having a text box that needs to be mandatory when a certain radio button is selected if the text box is not filled they can not submit the form.
radio button name is = complete
text box name is = remediation taken

when Complete is selected then Remediation Taken must be filled in before submitting if they hit submit without filling in Remediation i want a message pop-up stating must fill in Remediation before you can continue

can someone please help me with this
 
Textbox names cannot have spaces in them.

Anyway the easy way would be to run a validation function when the submit button is pressed.

Code:
<input type="submit" ... onclick="return validateComplete(this.form);">


Now for the validation:

Code:
function validateComplete(formObj)
{
if(formObj.elements.complete.checked == true)
{
   if(formObj.elements.remediationtaken.value!='')
   {
      return true;
   }
   else
   {
     alert("You must fill in Remediation");
     return false;
   }
}
return true;
}


----------------------------------
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
 
assuming jQuery is loaded
assuming the form has an ID of 'myForm'
Code:
$('#myForm').on('submit', function(e){
 var ok = false;
 var radio = $(this).find(':input[name="complete"]');
 var textField = $(this).find(':input[name="remediation taken"]');
 if( radio.is(':checked') ){
   if ( textField.val().length > 0 ){
     ok = true;
   }
 }
 if (!ok){
   e.preventDefault();
   return false;
 } else {
   return true;
 }
}
 
Save yourself the pain and frustration and just use a library to do this. You can create a dynamic HTML form from a JSON object that includes support for AJAX/POST methods and form field validation using Regular Expressions. It cannot get any easier than this. Full IE7 support too.

Webform-Toolkit

M. Brooks
 
Save yourself the pain and frustration and just use a library to do this. You can create a dynamic HTML form from a JSON object that includes support for AJAX/POST methods and form field validation using Regular Expressions. It cannot get any easier than this. Full IE7 support too.

Which is fine if you are happy that there will be no content for SE's to index?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
And I think mbrooks should be upfront in his posts that UE is the author of that particular library and so there is self-interest even though it is made freely available.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top