Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone.
I am developing a web based system and i want that my text boxes accept alphabet values only. how can i implement this with javascript or jquery kinldly show me some code please...

i have tried a function as follows but this function does work only in button and in one text box i want dynamic code that work everywhere whenever i will call to it.

What I have tried:

function onlyAlphabets() {
var regex = /^[a-zA-Z]*$/;
if (regex.test(document.getElementById("txtdetail").value)) {
return true;
} else {
alert("Alphabets Only Please.");
return false;
}
}
Posted
Updated 18-Dec-16 23:43pm
Comments
F-ES Sitecore 19-Dec-16 5:27am    
To make it "global" put code in a js file that does something like

$("input[type='text']").keyup(onlyAlphabets())

or however it is you instigate this code, then include that js file in the header for your site and it'll work for all inputs. You could also do something like

$("input[data-alphabet='true']").keyup(onlyAlphabets())

then you can add that attribute only to the boxes you want the validation on

<input type=text data-alphabet='true'>

HI ,
Try to create a generalize function , Like the one :

function onlyAlphabets(strTextboxIdentity) {
var regex = /^[a-zA-Z]*$/;
if (regex.test(document.getElementById( strTextboxIdentity ).value)) {
return true;
} else {
return false;
}
}

Use the above function wherever you want to check alphabet characters for textbox :

var Result = onlyAlphabets(txtdetail);
if(Result == false)
{
alert("Alphabets Only Please.");
return ;
}
else
{
// Your condition which is required here.
}
 
Share this answer
 
 
Share this answer
 
Thankx To All I have Done this with the following Code and it works for me...


function isAlfa(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)) {
alert("Allow Only Alphabets");
return false;
}
return true;
}

ASP.NET
<asp:TextBox ID="txt_Cat_Name" onkeypress="return isAlfa(event)" runat="server" CssClass="Text"></asp:TextBox>
 
Share this answer
 
v2

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