Web Analytics Made Easy -
StatCounter validtion no running successfully.Pls help - CodingForum

Announcement

Collapse
No announcement yet.

validtion no running successfully.Pls help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • validtion no running successfully.Pls help

    var str;
    str=document.frmSelect.txtCode.value;


    if((str.indexOf("*") == -1)||(str.indexOf("@") == -1))


    {
    alert(invalid character;

    }



    I want to valide that characters like @#$%^& should not be allowed when i dont use || validation goes fine but if i use || opeartor all value get accepted. am i not using the operator properly

  • #2
    If this is the whole code I wonder that it will works even with a simple if statement... To validate you have either to verify each element of the string or to delete the unwanted character onkeyup. In fact you better do both.

    The RegExp will help you a lot, and the script will be much shorter an wiser (some old folks here said so...)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      Actually there is much wrong with that code. The alert is not closed or delimited and the validation will fail because -1 indicates that it does not exist.

      For that validation to work it would need to look like this:
      if((str.indexOf("*") > -1)||(str.indexOf("@") > -1))

      However as Kor suggested, regular expressions would be better.
      Try this:
      Code:
      <script type="text/javascript">
        function chkStr(field){
         var str = field.value;
         var reg = /@|#|&|\^|\*/;
          if(str.match(reg)){
           alert('invalid character');
           field.value = str.substring(0,str.length-1);
          }
        }
      </script>
      </HEAD>
      
      <BODY>
      <form>
      <textarea onkeyup="chkStr(this)"></textarea>
      </form>
      .....Willy

      Comment


      • #4
        thanks

        thanks a lot that piece of code helped me out

        Comment

        Working...
        X