How can i check if a string contains any letter in javascript?
Im currently using this to check if string contains any numbers:
jQuery(function($){
$('body').on('blur change', '#billing_first_name', function(){
var wrapper = $(this).closest('.form-row');
// you do not have to removeClass() because Woo do it in checkout.js
if( /d/.test( $(this).val() ) ) { // check if contains numbers
wrapper.addClass('woocommerce-invalid'); // error
} else {
wrapper.addClass('woocommerce-validated'); // success
}
});
});
However i want it to see if it contains any letters.
4
Answers
You have to use Regular Expression to check that :-
Returns:
true
– if at least one letter is found (of any case, thanks to the/i
flag)false
– otherwise/i
– case insensitive flag(
/g
– global flag (allows multiple matches) – in this scenario it would be at least redundant, if not harmful for the performance. Besides it sets thelastIndex
property of regex (would be of concern if regex was used as a constant / variable and multiple tests were performed))Note:
would match any alphanumerical.
If one wants to add support for non-ASCII letters also, then they may use Unicode property escapes.
you can do this: