Forums

JS custom validation troubles

01 10 Dec, 2015
Hi there!
I've ran into trouble with custom validation.
About two ears ago I've used the custom JS rule for two forms on one page and they worked.
Then I've migrated to V5 and as far I remember there was some troubles.
After few months I've retested the projected and was very upset, because the forms doesn't work and validate.

The situation:
1). There are two separate forms on one page
2). I've put
validate['required','%customCheck'] F
line in class tag's of each input field (two fields on one page)
3). I've one common Load JavaScript event in the first form with the following code:

function customCheck(el){
    if (!el.value.test(/^[а-яА-ЯёЁa-zA-Z ąčęėįšųūžĄČĘĖĮŠŲŪŽ]+$/)) {

        el.errors.push("Используйте только кириллические символы или латинские");
        return false;
    } else {
        return true;
    }
}

The code checks if the user types his name in allowed languages (En - default, Lt & Ru - additional languages)

So, now hitting the submit button (doesn't matter which form) twice the user see: 1). the error message from title property and hitting twice loads the success message!

Need help solving this
Thanks in advance!
GreyHead 10 Dec, 2015
1 Likes
Hi 01,

You are using the CFv4 code with CFv5 - it needs to be updated.

In the Designer tab, open the Name element and put the Error message in the Title box; click the Validation tab and put customCheck in the Custom function box.

In the Setup tab change the JavaScript to this
function customCheck(el) {
  var regex, val;
  regex = /^[а-яА-ЯёЁa-zA-Z ąčęėįšųūžĄČĘĖĮŠŲŪŽ]+$/;
  val = jQuery(el).val();
  if ( regex.test(val) ) {
    return true;
  } else {
    return false;
  }
}
Save and test.

Please see this FAQ for more information.

I'm not sure if this will solve any problems with the two forms, please post again if it doesn't.

Bob

[[>> later : fixed typo <<]]
01 10 Dec, 2015
HI GreyHead!
Thank you for your reply!
I think you gave me the right answer. But now I see one problem - something is "holding" the validation. It does not matter what I'm entering in the input - it just throw the error message from the title. :/
01 10 Dec, 2015
It sticks always on

return false


statement...
GreyHead 10 Dec, 2015
Hi 01,

Sorry, I copied over a typo :-(

Please replace this line
  if ( regex.test(el) ) {
with
  if ( regex.test(val) ) {

Bob
01 10 Dec, 2015
Hi GreyHead!
I just few moments ago also have found it🙂
Thank you very much!
It seems, that everything is OK now
This topic is locked and no more replies can be posted.