Skip to content

Instantly share code, notes, and snippets.

@evgv
Last active December 6, 2016 11:48
Show Gist options
  • Save evgv/69e83b51833ca0d6fa0b2b2d975a41d0 to your computer and use it in GitHub Desktop.
Save evgv/69e83b51833ca0d6fa0b2b2d975a41d0 to your computer and use it in GitHub Desktop.
Magento. Add custom form validation pattern

Add custom form validation pattern

Resource for test regular expression.

Examples lookin for into js/prototype/validation.js on line 414 and there you can add newcustom validation pattern.

Structure
    ['{className}', '{error message}', function(v) {
        return Validation.get('IsEmpty').test(v) || /{regular expression}/.test(v);
    }],

where:

  • {className} - class name what will be added to field for checking field value on this pattern
  • {error message} - message what will see user if validation not passed
  • {regular expression} - regular expression for check field value

Validation for UA phone numbers like +38 (999) 999-99-99

    ['validate-phone', 'Please enter a valid phone number. For example +38 (999) 999-99-99.', function(v) {
        return Validation.get('IsEmpty').test(v) || /^(\+(38)\s)?(\(\d{3}\))?\s?(\d{3}[-. ]?\d{2}[-. ]?\d{2})$/.test(v);
    }],

Validation for RU phone numbers like a +7 (999) 999-99-99

    ['validate-phone', 'Please enter a valid phone number. For example +7 (999) 999-99-99.', function(v) {
        return Validation.get('IsEmpty').test(v) || /^(\+\d\s)?(\(\d{3}\))?\s?(\d{3}[-. ]?\d{2}[-. ]?\d{2})$/.test(v);
    }],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment