Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Expert,

I am uday satardekar,

In my asp.net website nridubai i am using javascript for validation

eg

JavaScript
var eventName=document.getElementById("<%=txtEventName.ClientID%>").value;

 if (eventName=="")
      {
                 alert("Name Feild can not be blank");
                 document.getElementById("<%=txtEventName.ClientID%>").focus();
                 return false;
      }


Its working fine for me.

now i want validation for special chars.Please give solution .

Thanks in advance.........
Posted
Updated 28-Jun-12 22:59pm
v3
Comments
E.F. Nijboer 12-Oct-11 7:08am    
You chose asp.net so why not use field validators then?
http://msdn.microsoft.com/en-us/library/aa479013.aspx
udusat13 12-Oct-11 7:19am    
Thankssssss..

To avoid server round trips, i am using client side scripting.
Sandeep Mewara 29-Jun-12 5:03am    
And the new edit/update was done for?

in OnClientClick event u can call the following function:

C#
function valid()
    {
    debugger;
     var s = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
     str=document.getElementById('<%=TextBox1.ClientID%>').value;
     for (var i = 0; i < str.length; i++)
     {
        if (s.indexOf(str.charAt(i)) != -1)
      {
         alert ("The box has special characters. \nThese are not allowed.\n");
        document.getElementById('<%=TextBox1.ClientID%>').value="";
         return false;
      }
    }
    }
 
Share this answer
 
v2
check the folllowing function
JavaScript
function avoid_Special()
{
 myString = new String("*&^%$#@") //list of special char.
 if(myString.indexOf(document.getElementById("txtOK").value) != -1)
 alert("Special char not allowed");
 return false;

}
 
Share this answer
 
v2
Comments
udusat13 12-Oct-11 7:20am    
Thanks for replying me,

I have modified my code like following

myString = new String("*&^%$#@") //list of special char.
if(myString.indexOf(eventName) != -1){
alert("Special char not allowed");
return false;
}

and its working only if first letter is special char,

Please guied me .
C#
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

 for (var i = 0; i < document.formname.fieldname.value.length; i++) {
   if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {
   alert ("Your username has special characters. \nThese are not allowed.\n Please remove them and try again.");
   return false;
   }
 }



Refer This link:
http://psacake.com/web/jb.asp[^]
 
Share this answer
 
Comments
udusat13 12-Oct-11 7:36am    
Thankssssssss.

Its working for me.

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