My alphabet uses characters other than a-z . . .

The ChronoForms alpha and alphanumeric validation uses a strict test and will allow only a-z, A-Z. For many languages this isn't useful.
 
Here are some workarounds:

Serverside validation

You can add a Custom Serverside Validation action to check your alphabet using code like this (change the $regex line and the 'input_name' to suit your needs):
 
<?php
$data =& $form->data['input_name'];
$regex = '/^[ÅÄÖa-z ._-]+$/i';
if ( isset($data) && $data ) {
  if ( preg_match($regex, $data) === 0 ) {
    $form->validation_errors['input_name'] = "Invalid data";
    return false;
  }
}
?>

Here's a second example using a Unicode Script specifier to select a whole range of characters. See here for a list of Unicode Scripts that you can use in place of \p{Greek}

<?php
$data =& $form->data['input_name'];
$regex = '/\A\p{Greek}+\Z/iu';
if ( isset($data) && $data ) {
  if ( preg_match($regex, $data) === 0 ) {
    $form->validation_errors['input_name'] = "Invalid data";
    return false;
  }
}
?>

Clientside validation in CFv4

You can create a custom validation by adding a validation function in a Load JS action in the ON Load event 
function customAlpha(el){
  if (!el.value.test(/^[ÅÄÖa-z ._-]+$/i)) {
    el.errors.push("Please only use alphabetic characters");
      return false;
  } else {
    return true;
  }
}
Then add this into the Class box of the element that you want to validate:
 validate['required','%customAlpha']
Again, change the regex expression and the error message to meet your needs.  Note that you must keep the a-z range to include unaccented characters. 

Clientside validation in CFv5

This is similar to CFv4 but you can now add the name of a custom validation function in the Custom Function the bottom of the Validation tab in an input element. This example is a check for characters in the Turkish alphabet.
In the Custom Function box add checkTurkish
In a Load JavaScript action in the form On Load event add code like this
function checkTurkish(el) {
  var pattern = /^[a-zA-Z0-9[şŞıİçÇöÖüÜĞğ\ \-_]+$/;
  return pattern.test(jQuery(el).val());
}

 

Category: CFv4 Validation

Comments:

You need to login to be able to post a comment.