Here I have created a JavaScript Method for validating the SPACE
no space allowed in first position and last position, there can be only one space in between any word
This function will Trim() the content and will check the space occurences if more than one space found then it will propmt "NOT VALID" else "VALID"
<script type="text/javascript">
function AllowSingleSpaceNotInFirstAndLast() {
var obj = document.getElementById('TextBox1');
obj.value = obj.value.replace(/^\s+|\s+$/g, "");
var CharArray = obj.value.split(" ");
if (CharArray.length > 2) {
alert("User name NOT VALID");
return false;
}
else {
alert("User name VALID");
}
return true;
}
</script></script>
Username: <input type="text" id="TextBox1" name="user" />
<input type="button" value="Submit" onclick="return AllowSingleSpaceNotInFirstAndLast();" />
Mark it as solution if it answer your question