Forums

How to validate one required

ksimba 06 Jan, 2009
Is it possible to validate one required with two text boxes? I need the user to either enter in a phone number or an email address or both before submitting the form. Cant seem to get this to work properly allthough the tooltip says that it would work with textbox/radio elements.

Here is my form code:
  <label>Phone Number
  <input type="text" name="phone" id="phone" />
  </label>
  <label>Email Address
  <input type="text" name="email" id="email" />
  </label>  <br /><br />
  <input type="submit" name="submit" id="submit" value="Submit" />


In line 7 of validation I have : email
In line 12 of validation I have: phone, email

Any help?
Thank you in advance
GreyHead 06 Jan, 2009
HI ksimba,

Wrap the two fields in a div, remove the entries from line 12 of the validation and manually enter the class in the last input of the group.
<div>
      <label>Phone Number
      <input type="text" name="phone" id="phone" />
      </label>
      <label>Email Address
      <input type="text" name="email" id="email" />
      </label>  <br /><br />
      <input type="submit" name="submit" id="submit" class='validate-one-required' value="Submit" />
</div>
That should work OK, though I've never tried it with inputs.

Bob
ksimba 06 Jan, 2009
Doesn't seem to work for me. Have any more suggestions? 😟
Max_admin 06 Jan, 2009
Hi, I think this rule will not happen unless the fields are with the same names, and you can't do this with buttons I think too!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
ksimba 06 Jan, 2009
Ok, I removed the code from the button and gave the elements the same name and still no luck. Any other suggestions? I am sure that some one has done this before. I just need to be sure that the form does not submit any blank values. The user should enter in a phone number or an email address or both. :?
GreyHead 06 Jan, 2009
Hi ksimba,

I hope you slept well!

I was taking a look at this until our power went off for a couple of hours :-(

It looks to me as though the function was not well ported over from Prototype to MooTools . . . I'll look again when I've cought up with myself.

Bob
GreyHead 06 Jan, 2009
Hi ksimba,

Not idea but I think this version work OK. I've added some ids and extra classes to your Form HTML.
<div>
    <label>Phone Number
        <input type="text" name="phone" id="phone" class='email' value='' />
    </label>
    <label>Email Address
        <input type="text" name="email" id="email" class='validate-1req email' value=''/>
    </label>
</div><br /><br />
<input type="submit" name="submit" id="submit" value="Submit" />
and then this validation code needs to go into the JavaScript Box.
 // add the new validator to the validators
 Validation.add('validate-1req', 
        'Please select one of the above options.',
    function (v,elm) {
		var options = $$('.'+elm.id.toString());
		for( i = 0; i < options.length; i++ ){
			if(options[i].value) {
	  			return true;
			}
		}
	});
This checks for any other inputs with classes the same as the id of the field with the validation class. Here that's 'email' so we've added the 'email class to both inputs. It should work with any number of text inputs in any order.

Bob
ksimba 07 Jan, 2009
Hello Bob,

After copying the code just as you have it and adding it per your directions, I still cant get it to properly validate. Everything looks right and makes sense but just wont act right *grrr* Any more suggestions? I even turned off the validation option in chronoforms thinking that there was a conflict but still no luck. The only other JavaScript being called is for swmenupro but even when disable the menu, It still doesnt validate. 😟
ksimba 07 Jan, 2009
Bob, we have a saying here in the US and it is: YOU ARE THE MAN!!! The code would not work when placed in the JavaScript box within chronoforms but when I added it directly to the validation.js, it worked like a charm. I think that is all I needed. Thank you so very much!
GreyHead 07 Jan, 2009
Hi ksimba,

Excellent, 'm sure that the code could be more elegant but if it works OK . . .

Bob

PS No idea why it wouldn't work for you in the JavaScript - I developed it in there and it worked OK. Maybe there is some other tweak in my MooValidation file that I've forgotten about.
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger