I have created JavaScript code for validation of a simple text field. The issue is when I want to skip spaces in field,
var strFilter = /^[A-Za-z]*$/;
var chkVal2 = document.getElementById("fname").value.replace(/^\s+|\s+$/g, "");
if ((!strFilter.test(chkVal2)) || (chkVal2 == "")) {
alert("Please enter a valid first name\r\n (only characters)");
document.getElementById("fname").style.background = "#DFE32D";
document.getElementById("fname").focus();
document.getElementById("fname").value = null;
return false;
}
Here I want, when the value is checked, it removes all spaces in ID. The script is going well, but it’s not removing spaces.
1 Answer