...

View Full Version : Validate radio button, and make sure text field is not blank



CBG
07-20-2012, 02:27 AM
Hi,

On my SMF Forum, registration form, I am using custom fields, and want to make sure if the value="0" is dotted, it make sure teh text field is filled in, if not give an error when they click register.



<input type="radio" name="customfield[cust_platfo]" class="input_radio" id="customfield_cust_platfo_0" value="0" >
<input type="radio" name="customfield[cust_platfo]" class="input_radio" id="customfield_cust_platfo_1" value="1" >
<input type="radio" name="customfield[cust_platfo]" class="input_radio" id="customfield_cust_platfo_2" value="2" >


Now if value="0" is dotted/selected I want the following text field to be completed



<input type="text" name="customfield[cust_playst]" maxlength="255" size="50" value="" class="input_text" />


I am also going to need the full code, to give out a javascript error, when they click on the register button, as in what do I put in the form code etc...

Thanks

Old Pedant
07-20-2012, 03:53 AM
Is there some reason you used those oddball names, with square brackets?

Also, is there some reason you gave each radio button an ID?

Also, you say "I have a forum...". Surely you mean you have a form? As in HTML <form>?

Anyway, when you don't show us the rest of the <form> or how you are doing the validation, it's hard to give you an answer. And we certainly can't answer your last question, about when "they click on the register button" when we don't know what your register button is (is it a submit button? a regular button? or what?) without the code.

And, finally, you sure don't mean you want to "give out a javascript error", because if they aren't paying attention to their JavaScript error console [and 98% of people don't] they will never see the error. I would *assume* you want an error message *DISPLAYED*. But how do you want it displayed? You should *NOT* just use a JavaScript alert( ) as those can be disabled by users.

So... More info, please, if you expect usable help.

jmrker
07-20-2012, 04:07 AM
Based upon 'Old Pedant's comments, this is my guess ...


<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
// For: http://www.codingforums.com/showthread.php?t=268332

function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
// return fnd; // return option index of selection
// comment out next line if option index used in line above
return str;
}
function showFld(fldValue) {
var styleDisplay = 'none';
if (fldValue == '0') { styleDisplay = 'block'; }
document.getElementById('Fld0').style.display = styleDisplay;
}
function validateForm() {
var flag = true;
if (getRBtnName('cust_platfo') == '0') {
if (document.getElementById('fldInfo').value == '') { alert('Information needed'); flag = false; }
}
return flag;
// comment above and uncomment below for testing purposes only
// alert('validate: '+flag); return false;
}
</script>

<style type="text/css">
.hide { display:none; }
</style>

</head>
<body>
<form name="myForm" action="" method="post" onsubmit="return validateForm()">
<input type="radio" name="cust_platfo" class="input_radio" onclick="showFld(this.value)" value="0" > X
<input type="radio" name="cust_platfo" class="input_radio" onclick="showFld(this.value)" value="1" > Y
<input type="radio" name="cust_platfo" class="input_radio" onclick="showFld(this.value)" value="2" > Z
<div id="Fld0" class="hide"> Information:
<input type="text" name="cust_playst" maxlength="255" size="50" value="" class="input_text" id="fldInfo" />
</div>
<p />
<input type="submit" value="Submit" />
</form>
</body>
</html>

To avoid a lot of reloading, use the testing code instructions at the end of the "validateForm()" function.
:)

CBG
07-20-2012, 10:49 AM
Ok, I have a form, on a SMF Forum, which is why the field names, are odd, as they are custom field made by the forum.

I am not wanting to hide or show a field, I am wanting to make sure the text field is filled in

jmrker
07-20-2012, 04:09 PM
Ok, I have a form, on a SMF Forum, which is why the field names, are odd, as they are custom field made by the forum.

I am not wanting to hide or show a field, I am wanting to make sure the text field is filled in

Remove the class='hide' and it should work for you.

Only 'X' will check for the blank field as you asked.

Concerning a SMF Forum problem, you might be able to ask them (???)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum