1

I have written rule.js file in following path.

app/design/frontend/package/theme/Magento_Ui/web/js/lib/validation/rule.js

add following code in rule.js

"mobiletendigit": [
 function(value) {
 return value.length > 9 && value.length < 11 
 && value.match(/^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$/);
 },
 $.mage.__('Please specify a valid 10 digits mobile number')
 ]

add mobiletendigit class to input field.

<input type="text" name="telephone" value="<?php echo $block->escapeHtml($block->getAddress()->getTelephone()) ?>" title="<?php /* @escapeNotVerified */ echo __('Phone Number') ?>" class="input-text required-entry mobiletendigit validate-digits <?php /* @escapeNotVerified */ echo $this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('telephone') ?>" id="telephone" data-validate="{required:true, 'mobiletendigit':true}">

add following tag to form.

data-mage-init='{"validation":{}}'
MGento
1,51910 silver badges22 bronze badges
asked Feb 16, 2018 at 11:02

3 Answers 3

4

Use like below code...

1) In input or select tag add our validaion with this code

data-validate="{required:true, 'validate-custom-name':true}"

2) Add js validation for "Validate-custom-name"

validation script

<script type="text/javascript">
require([
'jquery', // jquery Library
'jquery/ui', // Jquery UI Library
'jquery/validate', // Jquery Validation Library
'mage/translate' // Magento text translate (Validation message translte as per language)
], function($){ 
$.validator.addMethod(
'validate-custom-name', function (value) { 
return (value !== 'test'); // Validation logic here
}, $.mage.__('Enter Valid name'));
});
</script>
answered Feb 16, 2018 at 11:10
3
  • hi kisan, validate-custom-name should be validate-mobiletendigit... Commented Feb 16, 2018 at 11:30
  • Yes. But it is just for example. You can use your own. Commented Feb 16, 2018 at 11:39
  • <script type="text/javascript"> require([ 'jquery', // jquery Library 'jquery/ui', // Jquery UI Library 'jquery/validate', // Jquery Validation Library 'mage/translate' // Magento text translate (Validation message translte as per language) ], function($){ $.validator.addMethod( 'validate-mobiletendigit', function (value) { return (value !== '7715878743'); // Validation logic here }, $.mage.__('Enter Valid name')); }); </script> i am using this but not working Commented Feb 16, 2018 at 11:54
2
  1. Define this Js Code in view file :

    <script type="text/javascript">
     require([
     'jquery',
     'jquery/ui',
     'jquery/validate',
     'mage/translate'
     ], function($){ 
     $.validator.addMethod(
     'mobile-number-validation', function (value) { 
     var re = /^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$/;
     return !re.test(value);
     }, $.mage.__('Please specify a valid 10 digits mobile number.'));
     }); 
    </script>
    
  2. Now Just Add Validation Class in input field .

    <input type="text" class="mobile-number-validation" name="telephone" data-validate="{required:true}"/>
    
answered Feb 16, 2018 at 12:00
4
  • hi Sujeet its working but every time displaying wrong message Commented Feb 16, 2018 at 12:12
  • hi sujeet i want to add this on rule.js Commented Feb 16, 2018 at 12:16
  • @ErSarveshVTiwari what message it is showing ? Or May be check the pattern is correct OR not for 10 digits mobile number? Commented Feb 17, 2018 at 5:12
  • its working fine. i am looking its validation for knockout js page Commented Feb 18, 2018 at 6:53
1

It could help someone !

Un exemple for à phone number who contains exactly 10 digits and begin with 06 or 07 :

Field :

<input type="phone" name="phone" id="phone" value="<?= $block->escapeHtmlAttr($block->getFormData()->getPhone()) ?>" title="<?= $block->escapeHtmlAttr(__('Phone')) ?>" class="input-text" data-validate="{required:true, 'validate-mobilePhoneNumber':true}">

Js condition:

<script type="text/javascript">
 require([
 'jquery', 'jquery/ui', 'jquery/validate', 'mage/translate', 'mage/mage'
 ], function($){
 $.validator.addMethod(
 'validate-mobilePhoneNumber', function (value) {
 return /^0(6|7)[0-9]{8}$/.test(value);
 }, $.mage.__('Please enter a mobile phone number. Ex 06******** ou 07********.'));
 });
</script>
answered Jun 6, 2018 at 13:21

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.