Skip to content

Check if string contains any letter (Javascript/jquery)

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.

Answer

You have to use Regular Expression to check that :-

var regExp = /[a-zA-Z]/g;
var testString = "john";
            
if(regExp.test(testString)){
  /* do something if letters are found in your string */
} else {
  /* do something if letters are not found in your string */
}