1

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=""/>
</form>
<script type="text/javascript">
  var searchForm = new VarienForm('search-form');
</script>

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

1

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

| improve this answer | |
  • How to validate like accept number with any one special characters in phone field – Prathap Gunasekaran Nov 30 '18 at 12:20
1

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/

| 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.