javascript – Difference between validation and mask

Question:

I'm doing form validations. As for validation, everything goes well, but how to format the fields according to the typed data?

Example: in a date field, bars are added automatically, in a phone field, parentheses and dashes are added automatically. Are these additions called "masks"?

Answer:

If your function alters, controls the shape of a data, shapes its shape or imposes a certain format on your data, this is considered a mask.

E.g.: CPF – (000,000,000-00) / Date – dd/mm/yyyy

If your function checks if your data meets an expected result, this is considered a validation.

function verificaNum(valor) {
     if(isNaN(valor)) { // se não for um número
         console.log('não é um número! Valor inválido');
     } 
     else { // se for um número
         console.log('é um número! Valor válido);
     }
}

Note: isNaN returns a boolean whether or not the value is a number.

Ex:

 isNaN(123) //false
 isNaN('Hello') //true

But there may also be the case that you want to unify the two within the same role. It will depend on your purpose….

Scroll to Top