0

I'm trying to prevent the user from entering text input in a certain field.Now i'm using extjs panels, and i'm giving the function the record of the panel. The variable phone is being extracted, but i'm not being able to check it if it's formed completely of numbers or not. Any help please?

function saveHiring(record){
    var phone = record.getField("PHONE_NUMBER").getRealValue();
       var charCode = (phone.which) ? phone.which : phone.keyCode;

       if (charCode != 46 && charCode > 31 
         && (charCode < 48 || charCode > 57)){

           alert('no');
       }
       else{

    alert('yes');
}
}
mikeb
  • 709
  • 2
  • 9
  • 35
  • 1
    There is a very useful thing for this sort of operation called a regex if you want extensive validation including the number format. Or as Archer said you can limit the user input to only enter numbers – NSNoob Nov 26 '15 at 09:51
  • 1
    Just set the `type` of the field to `num`. That will make it only allow number input. – Reinstate Monica Cellio Nov 26 '15 at 09:51

1 Answers1

0

I see an XY problem here.

For other numbers: ExtJS Number field

For other number inputs (e.g. a count of something), please compare the official ExtJS documentation regarding Ext.form.field.Number. If you feed it with the details (minimum value, maximum value etc.), it will do most of the work for you regarding input validation.

Phone numbers

A phone number is not only digits. I would expect a program to accept +43-699/10565921 or +49(163)1 73 77 43 as a valid phone number. You should read A comprehensive regex for phone number validation and related posts.

Community
  • 1
  • 1
Alexander
  • 19,906
  • 19
  • 75
  • 162