Allow only characters and space in textbox

Allow only characters and space in textbox

Here, we will show how you can allow only characters and space to be typed in a textbox. This is some time requirement to only need characters and space in textbox.

First we create html for enter characters and space in textbox.

				
					<!DOCTYPE html>
<html>
    <head>
    <title>Allow only characters and space in textbox javascript</title>   
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
    </head>
    <body>
            <h3 align="center">Allow only characters and space in textbox javascript</h3>
            <br />
                        <label>Full Name</label>     
                        <input type="text" name = "full_name" id = "full_name" 
                         value="" onkeypress="javascript:return onlyCharSpace(event)">
    </body>
</html>
				
			

Above code we use onkeypress javascript event.

Below is javascript code for validate typed characters and space

.

				
					function onlyCharSpace(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || (charCode==32)) {
            return true;
        }
        return false;
    }
				
			

I hope this article helps you.
Thanks for visiting Inflay.com .