Condition to call mask!

Asked

Viewed 133 times

0

How can I read the first character typed in the input and the part of the condition be called a specific mask.

if(Se o primeiro caractere no input for letra){



}else{

$('#login_username').mask('(99) 9999-9999?9').focusout(function() {
	        var phone, element;
	        element = $(this);
	        element.unmask();
	        phone = element.val().replace(/\D/g, '');
	        if( phone.substring(0, 1) === '0' ) {
	            element.mask("9999-999-9999");
	        } else if (phone.length > 10) {
	            element.mask("(99) 99999-999?9");
	        } else {
	            element.mask("(99) 9999-9999?9");
	        }
	    }).trigger('focusout');
}

  • Swap the image with the pf code @Igorsiqueira

  • Ready! Done!.

1 answer

2


You can use regex with the script xregexp.

Then just give a match with the expression \p{L}. If it returns, it is true, and passes in condition.

-- Edited

Try this:

if (!isNaN(str.charAt(0))) // * charAt(0) é a primeiro char. verifica se ele não é um "Not-a-Number" - ou seja, um caractere válido para você.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.