Click here to Skip to main content
16,015,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a field nick i want to set some condition using javascript like the ones at the end of the post
what i want is javascript to Check if field nick has special characters like for example this sample "<" how do i do that
if (nick.value.length == 0)
{
    alert("wrong name");
    nick.focus();
    return;
}
if (nick.value.length >= 50)
{
    alert("wrong name");
    nick.focus();
    return;
}

Regards
Posted
Updated 1-Aug-11 1:30am
v2
Comments
Herman<T>.Instance 1-Aug-11 7:21am    
use regular expressions

1 solution

You can use something like this:
JavaScript
var splChars = "*|,\":<>[]{}`\';()@&$#%";
for (var i = 0; i < nick.value.length; i++) {
    if (splChars.indexOf(nick.value.charAt(i)) != -1){
    alert ("Illegal characters detected!"); 
    nick.focus(); 
}
 
Share this answer
 
Comments
akoyoon 1-Aug-11 7:36am    
can you explain to me the code and how it work
[no name] 1-Aug-11 8:01am    
if you have a little knowledge of JavaScript, you should be able to figure it out yourself.

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