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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Check boxes 1

Status
Not open for further replies.

JohannIcon

Programmer
Sep 3, 2002
440
MT
Dear All,

I have an input form where the user must enter his name, surname, e-mail address and choose from three topics, namely Conference, Leisure or Corporate. He can choose as many as he like, however, he has to choose one before submitting the form.

How can I do the validation on these checkboxes?

I came up with this code:-

If request.form("Conference") = "" then
er = 1
End IF

if er <> 1 then
If request.form(&quot;Leisure&quot;) = &quot;&quot; then
er = 1
End IF
end if

if er <> 1 then
If request.form(&quot;Corporate&quot;) = &quot;&quot; then
er = 1
End IF
end if

If er = 1 then
message = &quot;You have to choose at least one check box&quot;
end if

However, how can I stop the form from being submitted?

Thanks for your help and time
 
you can't really convert it - it's a different langugage - here are some examples that I've conjured up. You can study them and use them if you like, or search elsewhere. There are a lot of samples on the web, and also here at tek-tips:
Here's something simple:

<script language=&quot;JavaScript&quot;>
function ValidateForm()
{

if (document.frmQuickQuote.txtSchoolName.value ==&quot;&quot;)
{
alert(&quot;Please enter a school name.&quot;)
return false;
document.frmQuickQuote.txtSchoolName.focus()
}
if (document.frmQuickQuote.txtContact.value ==&quot;&quot;)
{
alert(&quot;Please enter a contact name.&quot;)
return false;
document.frmQuickQuote.txtContact.focus()
}
if (document.frmQuickQuote.txtPhone.value.length<10 | document.frmQuickQuote.txtPhone.value.length>12)
{
alert (&quot;Please check the telephone number you entered. Make sure it has an area code.&quot;)
return false;
document.frmQuickQuote.txtPhone.focus()
}

}
</SCRIPT>

Here is how the above code is called by the html on the form:

<form action=&quot;QuickQuoteProc.asp&quot; method=&quot;post&quot; name=&quot;frmQuickQuote&quot; onSubmit=&quot;return ValidateForm()&quot;>


Here's something a little more involved:

function isItDate(elm) {
if (elm.value.length < 10 || elm.value == &quot;&quot;) {
return false;
}
for (var i=0; i < elm.value.length; i++) {
if (i > -1 && i < 10) {
if (elm.value.charAt(2) == &quot;/&quot; || elm.value.charAt(5) == &quot;/&quot;) {
return true;
}

else if (elm.value.charAt(i) < &quot;0&quot; || elm.value.charAt(i) > &quot;9&quot;) {
return false;
}
}
}
}

//this function tests for an empty value in the box it's called on below
function isItEmpty(elm) {
if (elm.value == &quot;&quot; || //the &quot;|&quot; means &quot;OR&quot;
elm.value == &quot;null&quot;) {
return false;
}
}

//This is the main portion of the function - it incorporates all of the other functions
//outlined above and uses them on the text/combo boxes on the form before it is submitted.

function formValidation() {

var currentForm

currentForm=document.frmChangeEvent

if (isItEmpty(currentForm.txtEventName) == false) { //isItEmpty function defined above.
alert(&quot;Please enter a Name for this event.&quot;);
currentForm.txtEventName.focus();
return false;
}
else if (isItEmpty(currentForm.txtEventDescrip) == false) { //isItEmpty function defined above.
alert(&quot;Please enter a Description for this event.&quot;);
currentForm.txtEventDescrip.focus();
return false;
}
else if (isItDate(currentForm.txtEventDate) == false) {
alert(&quot;Please enter a date in the form dd/mm/yyyy.&quot;);
currentForm.txtEventDate.focus();
return false;
}
}


Here's how the above code is called in this case from the form's html

<form method=&quot;post&quot; action=&quot;EventsChange.asp?formUpdate=true&quot; name=frmChangeEvent onSubmit=&quot;return formValidation()&quot;>


Good luck. I'd also suggest buying a book. I like Nick Heinle and Bill Pena - Designing with Javascript. Great exapmles. It looks cryptic, but they do a good job of deciphering it. It was 35 bucks.




Ah say, there's somethin' a little &quot;eeeeeeee&quot; 'bout a boy who don't like basbawl...
 
Hello scroce,

I did this piece of code:-

if (user.Conference.value == &quot;&quot; || user.Conference.value == &quot;null&quot;) && (user.Leisure.value == &quot;&quot; || user.Leisure.value == &quot;null&quot;) &&
(user.Corporate.value == &quot;&quot; || user.Corporate.value == &quot;null&quot;)
{
alert(&quot;Please Choose at least one check box option!!&quot;)
user.Conference.focus()
user.Conference.select()
return false;
}

however it is giving me an error on the &&

why?
 
Ok I solved it, had to add an extra bracket:-

if ((user.Conference.value == &quot;&quot; || user.Conference.value == &quot;null&quot;) && (user.Leisure.value == &quot;&quot; || user.Leisure.value == &quot;null&quot;) &&
(user.Corporate.value == &quot;&quot; || user.Corporate.value == &quot;null&quot;))
{
alert(&quot;Please Choose at least one check box option!!&quot;)
user.Conference.focus()
user.Conference.select()
return false;
}

However the validation is not working and the form is still submitted
 
<script>
function checkForm(){
allInputs = document.getElementsByName(&quot;input&quot;)
oneChecked = false
message = &quot;&quot;

for (x=0; x<allInputs.length; x++){
if (allInputs[x].type == &quot;checkbox&quot;){
if(allInputs[x].checked){
oneChecked = true
}
}
else{
if(allInputs[x].value == &quot;&quot;){
message += &quot;\n- &quot; + allInputs.name
}
}
}
if (!oneChecked){
message += &quot;\n- Check at least one box&quot;
}
if (mess != &quot;&quot;){
alert(&quot;incomplete form&quot; + message)
return false
}
return true
}
</script>

<form name=&quot;myForm&quot; action=&quot;somePage.asp&quot; onSubmit=&quot;return checkForm()&quot;>
<input name=&quot;fName&quot;>
<input name=&quot;lName&quot;>
<input name=&quot;eMail&quot;>
<input type=checkbox name=&quot;Conference&quot;>
<input type=checkbox name=&quot;Leisure&quot;>
<input type=checkbox name=&quot;Corporate&quot;>
<input type=submit value=&quot;Submit&quot;>
</form>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
wow, that is quite a nice procedure!

thanks mate, i will try it
 
OOpps - error

if (mess != &quot;&quot;){
alert(&quot;incomplete form&quot; + message)
return false
}

should be

if (message != &quot;&quot;){
alert(&quot;incomplete form&quot; + message)
return false
}

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Its not working! When I check one box, it is still telling me that I have to check at least one box!

Here is my script:-

<script>
function checksubmit()
{
//check to see if Topic is empty
if (user.PName.value == &quot;&quot;)
{
alert(&quot;Please Enter a Name Here!!&quot;)
user.PName.focus()
user.PName.select()
return false
}

//check to see if Surname is empty
if (user.PSurname.value == &quot;&quot;)
{
alert(&quot;Please Enter a Surname Here!!&quot;)
user.PSurname.focus()
user.PSurname.select()
return false
}


//check to see if Topic is empty
if (user.email.value == &quot;&quot;)
{
alert(&quot;Please Enter an e-mail Here!!&quot;)
user.email.focus()
user.email.select()
return false
}

allInputs = document.getElementsByName(&quot;input&quot;)
oneChecked = false
message = &quot;&quot;

for (x=0; x<allInputs.length; x++){
if (allInputs[x].type == &quot;checkbox&quot;){
if(allInputs[x].checked){
oneChecked = true
}
}
else{
if(allInputs[x].value == &quot;&quot;){
message += &quot;\n- &quot; + allInputs.name
}
}
}
if (!oneChecked){
message += &quot;\n- Check at least one box&quot;
}
if (message != &quot;&quot;){
alert(&quot;incomplete form&quot; + message)
return false
}

return true
}
</script>


 
OOpps - two errors corrected - try just my code...

<script>
function checkForm(){
allInputs = document.getElementsByTagName(&quot;input&quot;)
oneChecked = false
message = &quot;&quot;

for (x=0; x<allInputs.length; x++){
if (allInputs[x].type == &quot;checkbox&quot;){

if(allInputs[x].checked){
oneChecked = true
}
}
else{
if(allInputs[x].value == &quot;&quot;){
message += &quot;\n- &quot; + allInputs[x].name
}
}
}
if (!oneChecked){
message += &quot;\n- Check at least one box&quot;
}
if (message != &quot;&quot;){
alert(&quot;incomplete form&quot; + message)
return false
}
return true
}
</script>

<form name=&quot;myForm&quot; action=&quot;somePage.asp&quot; onSubmit=&quot;return checkForm()&quot;>
<input name=&quot;fName&quot;>
<input name=&quot;lName&quot;>
<input name=&quot;eMail&quot;>
<input type=checkbox name=&quot;Conference&quot;>
<input type=checkbox name=&quot;Leisure&quot;>
<input type=checkbox name=&quot;Corporate&quot;>
<input type=submit value=&quot;Submit&quot;>
</form>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Here's my take on it

<script language=&quot;JavaScript&quot;>
function CheckBoxes(){
var selection=0
for (i = 0 ; i < document.myform.elements.length ; i++){
oElement = document.myform.elements;
if (oElement.type.toUpperCase()== &quot;CHECKBOX&quot;){
if(oElement.checked==true){selection=+ 1;}}}
if (selection<1){alert(&quot;Please choose one&quot;);return false}}
</script>
<form name=myform action=page.asp onSubmit=&quot;return CheckBoxes()&quot;>
<input type=checkbox name=a value=1>
<input type=checkbox name=b value=2>
<input type=checkbox name=c value=3>
<input type=submit>
</form>

 
HI wolf00,

Thanks for the code. I tried to make an explanation out of it tough but I did not manage.
 
JohannIcon - Do you need more explanation?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top