Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to check space and special characters in textbox using a js file .
Posted
Comments
Kornfeld Eliyahu Peter 1-Apr-14 1:59am    
Did you heard about regex?
Snesh Prajapati 1-Apr-14 2:47am    
You will get a lot of resources on Google. Please use it.
Member 11407188 24-Aug-15 7:07am    
fvdvsdfvdsfv

C#
//This functions allows user to enter alphabets (A-Z, a-z) and special characters *.*, *-* (dot, dash)
//Method : onkeypress = "return ValidateUsernameOnly(event);"
function ValidateUsernameOnly(evt) {
    evt = (evt) ? evt : event;
    var characterCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
  ((evt.which) ? evt.which : 0));
    if (characterCode > 31 && (characterCode < 65 || characterCode > 90) && (characterCode < 45 || characterCode > 46) &&
    (characterCode < 97 || characterCode > 122) && (characterCode < 48 || characterCode > 57)) {
        return false;
    }
    return true;
}
 
Share this answer
 
Hi,

you can call this function like
JavaScript
function validate() {
            var TCode = document.getElementById('TBtext').value;

            if (/[^a-zA-Z0-9\-\/]/.test(TCode)) {
                alert('Input is not alphanumeric');
                return false;
            }

            return true;
        }


HTML Call:

ASP.NET
<asp:textbox id="TBtext" runat="server" onblur="validate();" xmlns:asp="#unknown"></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