Results 1 to 2 of 2

Thread: How to avoid special characters using javascript

  1. #1
    Junior Member
    Join Date
    Feb 2016
    Posts
    4

    How to avoid special characters using javascript

    I am making one website for my client, I had completed the task what he asked from me, but on my meeting with him for showing the demonstration he asked me to insert the validation I told me fine. No problem!

    Now the problem is the code of javascript for validation below is working correct, but while I am trying to avoid server round trips by using client side scripting. It is showing error for the especial charrater.

    Code:
    var eventName=document.getElementById("<%=txtEventName.ClientID%>").value;
     
     if (eventName=="")
          {
                     alert("Blank field");
                     document.getElementById("<%=txtEventName.ClientID%>").focus();
                     return false;
          }
    Any one is with the solution to resolve this coding.

  2. #2
    Junior Member
    Join Date
    Feb 2016
    Posts
    9

    Re: How to avoid special characters using javascript

    Code:
    var iChars = "!@#$%^&*()+=-[]\\\';,./{}|":<>?";
      for (var i = 0; i < document.formname.fieldname.value.length; i++) 
    {
       if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) 
    {
       alert ("You are using special characters. \n Not allowed.\n Please do remove it and try again.");
       return false;
    }
    }
    The above code will resolve your problem.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •