regex match letters and spaces code example

Example 1: javascript regex only letters and spaces

//regex expression:
var reg_name_lastname = /^[a-zA-Z\s]*$/;

//Validation to the user_name input field
if(!reg_name_lastname.test($('#user_name').val())){ //
    alert("Correct your First Name: only letters and spaces.");
    valid = false;
}

Example 2: regex for letters and spaces

/* Note this works with the onkeyup function in html with js */

function lettersandSpacesOnly(input){
    var regexs = /[^a-z ]*$/gmi;
    input.value = input.value.replace(regexs, "");
  }

Tags:

Misc Example