I need to validate my input field that need to contain only number or letter, not both of them. For example:

13131 or LosAngeles are valid.

Los131 is invalid

Here is my form:

<form id="search-form" method="post" action="">
  <lebel>Search For</label>
  <input type="text" class=""/>
<script type="text/javascript">
  var searchForm = new VarienForm('search-form');

How can I do that ? I really appreciate your help.

2 Answers 2


There is no native validation class available in magento validation lib.

Use this below code

Validation.add('validate-test','your message for validation error',function(v){
    return Validation.get('IsEmpty').test(v) || (!isNaN(parseNumber(v)) && /^\s*-?\d*(\.\d*)?\s*$/.test(v)) || /^[a-zA-Z]+$/.test(v);

Add this script in your js file.

Refer This Link

  • How to validate like accept number with any one special characters in phone field Nov 30, 2018 at 12:20

use this class in your input field

class = "validate-alpha" -- > Character only
class = "validate-number" -- > Number Only

For more refer http://inchoo.net/magento/out-of-the-box-form-validation-in-magento/

Your Answer

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

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