Click here to Skip to main content
15,040,003 members
Please Sign up or sign in to vote.
1.89/5 (3 votes)
See more:
I have this code:
C++


C#
$('#textName').keypress(function (e) {
        var regex = new RegExp("^[a-zA-Z]+$");
        var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
        if (regex.test(str)) {
            return true;
        }
        else
        {
        e.preventDefault();
        alert('Please Enter Alphabate');
        return false;
        }
    });
Posted
Updated 26-Apr-18 20:18pm

Try this:
^[a-zA-Z\s]+$

\s for one space
Read more: The 30 Minute Regex Tutorial[^]
   
you have id called txtName
HTML
<input type="text" name="name" id="txtName"></input>

so change
C#
$('#textName').keypress(function (e) {

to
C#
$('#txtName').keypress(function (e) {

And also use the Regex expression as /^[a-zA-Z\s]+$/
Ref :Jquery Validation: allow only alphabets and spaces[^]
   
v2
//Allowed only characters & spaces. Ex : Jayant Lonari
if(!/^[a-zA-Z\s]+$/.test(NAME))
{
//Throw Error
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900