Results 1 to 4 of 4

Thread: javascript validation

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    10

    Thumbs down javascript validation

    Hi all.

    I have a slite problem with javascript

    Lets say, i have variable, that are string

    Code:
    var = "1234567890abced"
    And i need to check on click event, for textbox, if the pressed key matches some of the chars inside our string variable.

    Can someone help me out at this?

  2. #2
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Re: javascript validation

    Hello

    if you want to check only one char I prefer this way more than using regExp

    HTML Code:
    <script type="text/javascript">
    var fineChars = '123456abcdef';
    function checkChar(inputField)
    {
       var pressedChar = inputField.value.substring(inputField.value.length - 1, inputField.value.length);
       if (fineChars.indexOf(pressedChar) == -1) {
            var newValue = inputField.value.substring(0, inputField.value.length - 1);
            inputField.value = newValue;
       }
    }
    </script>
    <input type="text" onkeyup="checkChar(this)" />
    regards

    Feras Jobeir

  3. #3
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Re: javascript validation

    Hello

    If you want to check a char only, I prefer to try this more that using RegExp

    HTML Code:
    <script type="text/javascript">
    var fineChars = '123456abcdef';
    function checkChar(inputField)
    {
       var pressedChar = inputField.value.substring(inputField.value.length - 1, inputField.value.length);
       if (fineChars.indexOf(pressedChar) == -1) {
            var newValue = inputField.value.substring(0, inputField.value.length - 1);
            inputField.value = newValue;
       }
    }
    </script>
    <input type="text" onkeyup="checkChar(this)" />
    regards

    Feras Jobeir

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    10

    Re: javascript validation

    Tnx for your replys guys, but i managed to do this on my own..

    This is how i did it:

    Code:
    onkeyup="input_res(this,'abC chars to be allowed')
    
    
      function input_res(object,validation){ 
                    var result = "";
                    for (i=0; i < object.value.length; i++) {
                        x = object.value.charAt(i);
                        if (validation.indexOf(x,0) != -1) {
                            result += x;
                        }
                    }
                    object.value = result
                }
    Good luck to everyone

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
  •  



Click Here to Expand Forum to Full Width