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>   
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
    </head>
    <body>
        <div class="container my-4">
            <h3 align="center">Allow only characters and space in textbox javascript</h3>
            <br />
            <div class="row">
                <div class="col-md-5">
                    <div class="form-group">
                        <label class="col-form-label">Full Name<span class="text-danger"></span></label>     
                        <input type="text" class="form-control" name = "full_name" id = "full_name" 
                         value="" onkeypress="javascript:return onlyCharSpace(event)">
                    </div>          
                </div>
            </div>    
        </div>
    </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 .