Forums

Comparing two form fields in validate

baalwww 16 Jul, 2008
I have a typical Contact Us form, with two fields Email and Verify Email. I need the validation to compare the two, and fail if they are not equal.
Max_admin 16 Jul, 2008
ok, step #1- add this class to the tag of the verify email field:

class="validate-the_same_value" title="email_field_id"

open mooValidation.js under components/com_chronocontact/js/

after this line :
Validation.addAllThese([

add :

['validate-the_same_value', 'Please use letters only (a-z) in this field.', function (v) {
return Validation.get('IsEmpty').test(v) || ((elm.value == $(elm.getProperty('title')).value)? true : false )
}],

Let me know!

Cheers

Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
baalwww 16 Jul, 2008
My two fields now look like this:
<tr>
  <td style='width: 120px'>Email:</td>
  <td><input type='text' class='required' name='email' 
    value='<?php echo $_POST['email'];?>'></td>
</tr>
<tr>
  <td style='width: 120px'>Verify email:</td>
  <td><input type='text'  name='vemail' 
    class='validate-the_same_value' title='email'></td>
</tr>
and the line in the code, right before the 'required' code is...[code]['validate-the_same_value',
'This field must contain the same value as Email.',
function (v) {
return Validation.get('IsEmpty').test(v)
|| ((elm.value == $(elm.getProperty('title')).value)
? true : false )
}],
['required[/code]and when they're not equal, the form still posts.
baalwww 17 Jul, 2008
Here's what I've done to check it...

I noticed no matter what I put in Verify Email, it wasn't verifying. So I put an alert at the start of the verification, and it never hits it.

Then I noticed that the Verification library was set to Prototype, so I changed it to mootools ... after which NOTHING verified, ever the empty required fields.
Max_admin 17 Jul, 2008
Hi,

Ok, actually you should also have id='email' in the EMail field tag!

next, if this didnt work, try this, remove this from the validation function :
|| ((elm.value == $(elm.getProperty('title')).value)? true : false )


then test that the Verify email field is getting validated for being not Empty and all other validations are fine, if it worked then the problem is with this last part and we will fix it, sorry but I didn't set on my office PC to check this yet!

Cheers

Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
baalwww 17 Jul, 2008
I'm afraid we'll need to take this one step at a time... I did add the 'id' to the email tag, I had noticed that the code was looking for it and it wasn't there. That didn't make a difference.

The fact is, I have backed out the entire addition from the .js file, putting it back to the way it was. The two fields prior to the email fields in my form are both required. When I set validation to prototype, and attempt to tab from the first empty field to the second, I am told that the field is required. When I set validation to use mootools and do the same, there is no validation.

Whatever is preventing mootools from validating, I expect, is making us think the new routine is not working too.
baalwww 17 Jul, 2008
Can you give me some idea of how to troubleshoot why the mootools verification setting isn't verifying?
baalwww 18 Jul, 2008
Ok. Have mootools validate working now. Turns out my default editor was not treating the end-of-line correctly.

So, now all the normal validation works. What's not happening, is the new routine is not being invoked by class=... in the tag. I even changed it to have the validation return a value no matter what, to cause the validation to fail, and... nothing.
GreyHead 19 Jul, 2008
Hi Baalwww,

Please check the page source and make sure that there is only one class attribute in the tag. Is the page on-line so I can take a quick look?

Bob
GreyHead 19 Jul, 2008
Hi baalwww,

I took a quick look and I can't see how the script knows which field to compare with . . . maybe there's a parameter missing.

Bob

Later: looking at the the help page Maybe you need:
Validation.addAllThese('validate-the_same_value', 'This field must be the same as the email field.', {
    equalToField : 'email'
});
Bob
baalwww 19 Jul, 2008
The Verify Email field passes a Title of 'email'. The script uses the title field of the verified field to use as the id of the field to compare, and the email field has an id of 'email'.

However, that's not the problem.

If I just remove all of the test in the script, and replace it with returning a value to fail the validation, it does not... it's not being invoked by "class=" in the verified field.
Max_admin 19 Jul, 2008
Hi baalwww,

did you try Bob's last code edit above ? maybe it will work ? whats the main problem we have now ? you say that class is not triggered, what if you added class="required" to the verify email address field, does it get validated ?
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
baalwww 19 Jul, 2008
Hi. Okay, here's where I am:

1. I changed the class to required, and it DOES fire.

2. I changed class back to the original, and changed the code in the validation routine to return false, and it DOES fire and fail validation.

So, it appears to be a problem with the code. I'm suspecting that
elm.value == $(elm.getProperty('title')).value)
is not evaluating.

I tried putting an alert just prior to the return, to see what it is evaluating to, and I get an error in firebug that elm does not exist.
Max_admin 19 Jul, 2008
hi,

try replacing function(v) with : function (v,elm)

??

Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
baalwww 19 Jul, 2008
Nothing is ever that easy ... until now. Yup, that did it!

In looking at it before, I figured it needed elm as a parm... but thought that just adding it couldn't work, because how would the sending entity know to include it. Overthinking, I guess.

Thanks!! :cheer:
baalwww 19 Jul, 2008
One final note... I changed it from

return Validation.get('IsEmpty').test(v) || ...


to

return !Validation.get('IsEmpty').test(v) || ...


because otherwise simply leaving the verify field empty, when the email field is not, passes validation. I suppose another way to do it would be to remove that from the validation routine, and add 'required' to the class=
Max_admin 20 Jul, 2008
Thanks for sharing this and for testing this with us!🙂

Best regards,

Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
disciple 27 Jul, 2008
Hi, can you please look at my code - I did whatever needed (at least I think so), but it still doesn't work:
code:
<label>Password
<input type="password" name="password" id="password"/>
</label>
<label>Verify Password
<input class="validate-the_same_value" title="password" type="password" name="passwordagain" id="passwordagain" />
</label>

mooValidation.js
Validation.addAllThese([
['validate-the_same_value', 'Two passwords must be identical', function (v,elm) {
return (!Validation.get('IsEmpty').test(v) || ((elm.value == $(elm.getProperty('title')).value)? true : false ));
}],
Max_admin 27 Jul, 2008
hi disciple,

all seems fine to me, I didn't test that code yet so no clue why it doesnt work, does all other validation types work fine with you ? can you add this code in moovalidation but after the required validation and not the first one ?

Cheers

Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
disciple 27 Jul, 2008
Hi, thank you for your reply. Everything else works fine, I've added after and still no result.
Now the code looks like this:
Validation.addAllThese([
['required', 'This is a required field.', function(v) {
return !Validation.get('IsEmpty').test(v);
}],
['validate-the_same_value', 'Two passwords must be identical', function (v,elm) {
return (!Validation.get('IsEmpty').test(v) || ((elm.value == $(elm.getProperty('title')).value)? true : false ));
}],
['validate-number', 'Please enter a valid number in this field.', function(v) {
return Validation.get('IsEmpty').test(v) || (!isNaN(v) && !/^\s+$/.test(v));
}],
['validate-digits', 'Please use numbers only in this field. please avoid spaces or other characters such as dots or commas.', function(v) {
return Validation.get('IsEmpty').test(v) || !/[^\d]/.test(v);
}],
Max_admin 27 Jul, 2008
I'm puzzled, I can't test it right now but will do soon, may be baalwww can post his moovalidation file for us here or you can PM him and see ?

Cheers

Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
disciple 27 Jul, 2008
I PMed him - hope he will answer cause I'm stuck. Thank you very much.
cjmicro 19 Aug, 2008
Is there any way one of you can post the final working code and js for this, because I have tried and tried and cannot get it to work. If you can give me an example of the whole thing I can try again. Please.... I'm really trying but I'm so not a programmer. 🙄

Cheryl
This topic is locked and no more replies can be posted.