Can't use regular expression

GreyHead 01 Jul, 2010
Hi Lara_Shadow,

Please re-read the post you linked to. The code went into the Form JavScript box. No need to hack any files.

Bob
Lara_Shadow 01 Jul, 2010
I am using this in Form Java Script

Validation.add('validate-phone-us', 'Please enter a valid phone number xxx-xxx-xxxx', function(v) { 
            var regex = /^\d{3}-\d{3}-\d{4}$/; 
            return Validation.get('IsEmpty').test(v) || regex.test(v); 
});

and this in HTML

<input class="validate-phone-us" maxlength="150" size="30" title="Please enter in the formate xxx-xxx-xxxx" id="text_3" name="text_3" type="text" />


Now getting

Validation is not defined

error
GreyHead 01 Jul, 2010
Hi Lara_Shadow,

Sounds as though the ChronoForms validation files aren't being loaded. Try setting 'Load ChronoForms css/Js Files' to yes and/or setting Validation 'On' in the validation tab.

Bob
Lara_Shadow 01 Jul, 2010
Actually both are set to YES

Following the snap shot from fire bug.

Showing error on line no 92
It is showing my JS validation in CDATA.
GreyHead 02 Jul, 2010
Hi Lara_Shadow,

Ah, thanks, looks as though we need to delay the validation until the other code has loaded. Put this wrapper round the script and see if that does the trick:

window.addEvent('domready', function() {
  Validation.add('validate-phone-us', 'Please enter a valid phone number xxx-xxx-xxxx', function(v) {
    var regex = /^\d{3}-\d{3}-\d{4}$/;
    return Validation.get('IsEmpty').test(v) || regex.test(v);
  });
});

Bob
Lara_Shadow 12 Jul, 2010
Now I getting this one error message.
GreyHead 18 Jul, 2010
Hi Lara_Shadow,

My apologies, the script I posted was for an old version of ChronoForms using a different validation library.

This script works OK with the present version:
window.addEvent('domready', function() {
  var phone = new LiveValidation('phone');
  phone.add(Validate.Format, { 
    pattern: /^\d{3}-\d{3}-\d{4}$/,
    failureMessage: 'Please enter a valid phone number'
  });
});
The input in the Form HTML is
<input class="cf_inputbox" maxlength="150" size="30" title="" id="phone" name="phone" type="text" />
and the result is:

[attachment=0]18-07-2010 16-06-28.png[/attachment]

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