0

I used Magento phone number validation validate-length maximum-length-11 minimum-length-11 validate-digits for phone number but i want to first digit starting with(7,8,9) and after that 9 digit will come how i restrict ?

  • how many digit altogether? – Adarsh Khatri Sep 30 '15 at 12:35
  • I got a solution check my answer – Magento 2 Sep 30 '15 at 12:40
1

i got answer to my question

<input type="text" name="billing[telephone]" value="<?php echo $this->escapeHtml($this->getAddress()->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?> validate-mobileno" id="billing:telephone" placeholder="Mobile"/>

<script type="text/javascript">
//<![CDATA[
if(Validation) {
Validation.addAllThese([
['validate-mobileno','Enter correct mobile number (Eg:919986858483)',
function(v){
var timePat ="^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}9[0-9](\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$";
// var matchArray = v.match(timePat);
if(v.length > 0){
if(v.length !=12){
    return false;
   }else if(v[0] != 9 || v[1] != 1 ){

    return false;
   }else if(v[2]!=9 && v[2]!=8 && v[2]!=7){

    return false;
   }


return true;

}else {
return false;
}

}
]])};
 var dataForm = new VarienForm('form-validate', true);
//]]>
</script>
| improve this answer | |
0

Add your own validation class

Validation.add('Your Class Name','Your Validation Message',function(v){
    return "check Your patterns here";
});
| improve this answer | |
0

Create this file custom-validate.js and place in js folder.

Validation.add('validate-phoneLax', 'Please enter a number starting from 7 or 8 or 9 and should be exactly 11 digits. For example 71234567890', function(v) {
    return Validation.get('IsEmpty').test(v) || /^7[0-9]{10}$/g.test(v) || /^8[0-9]{10}$/g.test(v) || /^9[0-9]{10}$/g.test(v)
})

Now just need to call this file from xml so that it is added in that particular page. If you want it to be in every page then just add in local.xml

<default>
    <reference name="head">
        <action method="addJs"><script>custom-validate.js</script></action>
    </reference>
</default>

Now when you have validate-phoneLax class name in input filed it should validate as you expect.

Remember

We are overriding phoneLax validation class.

I haven't tested but should work.

More Reading

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.