In magento how to give validation to telephone field in contact us page

telephone field should accept only numbers and - symbol other should not accept similarly name field should not allow special character and white space


To validate an input fiel, you just need a varienForm:

<script type="text/javascript">
     var loginForm = new varienForm('loginForm');

This form registers on the submit event of the <form>. Then you just need to add the validate- class you want to use, have a look into Validation.addAllThese() in /js/prototype/validation.js:414 or into the great article fishpig wrote.

  • I have already used this two validate-number( Please enter a valid number in this field)validate-phoneStrict Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890) but my need is it should accept num like 1234566778 or (123)-23456 or 123-4567 or +54534343434
    – vidhya
    Jan 16 '14 at 12:49

Inchoo wrote a great article about form validation that you can read here.

On the frontend you can add a class to the input, for example validate-phoneLax. In the controller receiving the post values you can use Zend validation like, for example,

if (!Zend_Validate::is($postdata['telephone'], 'NotEmpty')) {
   $errors[] = $helper->__('Please enter a phone number');

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.