Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,


i am taking two textbox for password validation and in those textboxes, user can enter only alpha numeric value i.e [a-z/A-Z/0-9]. and user can't enter any special characters like @,!,$,# etc..

if any one knows about it then pls help...


Regards,

Raj
Posted
Comments
Vijay Walunj,Navi mumbai 21-Sep-12 2:33am    
You need to make your condition test a regexp, not a string:

if(!/^[a-zA-Z0-9]+$/.test(name)){ ...
meaning:

^ -- start of line
[a-zA-Z0-9]+ -- one or more characters/numbers
$ -- end of line
or you could search for the inverse of that, which is "any non-accepted character":

if(/[^a-zA-Z0-9]/.test(name)){

Hello

U can use the javascript validation for this.

function ValidateUserName(ControlID) {
var Pattern = /^[0-9a-zA-Z]*$/;
var ctrl = document.getElementById("txtPassword")
//var ctrl = ControlID == undefined ? : ControlID;
if (ctrl.value == '' || ctrl.value.search(Pattern) == -1) {
window.alert("Special character not allowed.");
ctrl.value = "";
return false;
}
return true;
}

Use this script and place the on keypress event.
 
Share this answer
 
Hi,
U can use regular expression validator for that....
And provide validation expression as
"[A-Za-z1-9][A-Za-z0-9]*"
 
Share this answer
 
soln1:
XML
<script type="text/javascript">
    function validate() {
        var name = document.getElementById("name").value;
        var pattern = /^[A-Za-z0-9 ]{3,20}$/;
        if (pattern.test(name)) {
            alert(name +" has alphanumeric value");
            return true;
        } else {
            alert("Name is not valid.Please input alphanumeric value!");
            return false;
        }
    }
</script>
 
Share this answer
 
 
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