2

I have form with text field in Magento 1.9. I don't want to accept spaces at starting in text field in validations. Whenever I type space, it is not validating but don't want to accept space at first need to enter any characters in that field.

$('#number').keypress(function (e) {
       var regex = new RegExp("^[0-9\b]+$");
       var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
       if (regex.test(str)) {
           return true;
       }

       e.preventDefault();
       return false;
});

For number i wrote like this but space how to write code.

0

if you are asking for admin forms then here is my answer add class validate-alphanum

    $fieldset->addField('feild_id', 'text', array(
        'label'     => Mage::helper('helper_alias')->__('label'),
        'name'      => 'feild_name', 
        'class'     => 'required-entry validate-alphanum' ,//Add class
        'required'  => true,
    ));

For js use this condition use trim()

   if (regex.test(str.trim())) {
       return true;
   }
| improve this answer | |
  • what is regex? i want only avoid spaces at starting not all – User0434 Dec 13 '17 at 7:27
  • @Nadh you can use trimLeft() for this – Murtuza Zabuawala Dec 13 '17 at 7:38
  • it is taking entire field spaces not taking as starting – User0434 Dec 13 '17 at 8:46

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.