0

In Magento 1, I used the below javascript code inside of a template to add my own validation rule:

// Add new validation class for the name. Also supports names with two hypen in the name e.g. Hans-Maier-Friedrich.
// Also supports umlauts and some special chars like é or á
Validation.add('validate-name','Please only use (A-Z a-z àèéáäÄüÜöÖ) in this field.', function(v) {
 return Validation.get('IsEmpty').test(v) || /^[A-Za-zàèéáäÄüÜöÖ]+-?[A-Za-zàèéáäÄüÜöÖ]+-?[A-Za-zàèéáäÄüÜöÖ]+$/.test(v);
});

How does it work in Magento 2?

asked Nov 5, 2019 at 10:36

1 Answer 1

1

You can try something like below code, please replace validation rule and error message

<script type="text/javascript">
 require([
 'jquery', 'jquery/ui', 'jquery/validate', 'mage/translate', 'mage/mage'
 ], function($){
 $.validator.addMethod(
 'validate-name', function (value) {
 return /^0(6|7)[0-9]{8}$/.test(value); // your validation rule here
 }, $.mage.__('Your error message.'));
 });
</script>
answered Nov 5, 2019 at 10:49
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.