Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    trouble validating input with regExp

    so I am trying to do a very simple validation with regular expression. but code so far is always returning false or basically not making a match at all.

    I am just trying to match that the inpput is strictly alphanumeric characters with '_' , '.', and @ the underscore, period and @ I want to be allowed but not required.

    Here is what I have so far:

    Code:
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" ></script>
            <script>
    
            $(document).ready(function()
            {
                $('input').attr('onblur', 'validateText(this);');
            });
           //comment         /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
          //comment         /\w+\.+@/
            var patt = new RegExp('/\w/','i');
            //var ck_input = /^[A-Za-z0-9@.]$/;
            function validateText(input_id){
                var name = input_id.getAttribute("name");
                var input = document.getElementsByName(name)[0].value;
                alert('name: '+name + ' input: ' + input);
                if(!patt.test(input)){
                    alert('please input alphabetical and numerical characters only');
                }
            }
        
            </script>
    both alerts will fire for me and the first one will provide data I am expecting, but the 2cd alert will fire no matter what I put in the input field.

    Any ideas?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Overly complex, though I don't see where the error is from.
    Code:
            $(document).ready(function()
            {
                $('input').attr('onblur', validateText );
            });
            function validateText( )
            {
                alert('name: '+this.name + ' value: ' + this.value);
                var patt = /^[\w@.]$/;            
                if(!patt.test( this.value ) )
                {
                    alert('please input letters, digits, underline, period, or @ sign only');
                }
            }
    A slightly simpler way: Make a negative test, instead:
    Code:
            function validateText( )
            {
                alert('name: '+this.name + ' value: ' + this.value);
                var patt = /[^\w@.]/;            
                if( patt.test( this.value ) )
                {
                    alert('please input letters, digits, underline, period, or @ sign only');
                }
            }
    And of course I have to give the standard warning that alert( ) is obsolete and should be avoided except when debugging.
    Last edited by Old Pedant; 01-15-2013 at 07:51 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    By the by, no matter what, this code sequence is a bad idea:
    Code:
            function validateText(input_id) /* it is NOT an "ID"!  It is a REFERENCE to an <input>! */
            {
                var name = input_id.getAttribute("name");
                // what happens if you have more than one <input> with the same name?
                // but here is the funny part:  If there is only one element with the name,
                // now input and input_id.value *ARE ONE AND THE SAME THING!*
                var input = document.getElementsByName(name)[0].value;
    Even if you insist on using
    Code:
           $('input').attr('onblur', 'validateText(this);');
    instead of what I showed, then it would make more sense to write that as:
    Code:
            function validateText(input_obj) 
            {
                var name = input_obj.name;
                var input_val = input_obj.value;
                if ( ! patt.test( input_val ) ) ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    thanks a lot for the help, seems that a parameter had to be passed to my function to get it to read the object and its values. Other than that it ran perfect.

    I realize that input is an object when passed but I didnt realize that I didnt need to specify getting an attribute to target it. Good to know about that now.

    Thanks for your help


  •  

    Posting Permissions

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