SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Check letters, numbers and special characters

    Hi there.

    I need ur appreciated help.

    This is the function CheckmyForm.

    I need check that you enter only letters, numbers and special characters ",", "." and ";" in the TextareaS1 field.

    Can u help me?

    Code:
    function CheckmyForm() {
         
       if ( myform.DatesT1.value.length > 0 && myform.TextareaS1.value.length > 0 ) {
            alert("OK!.");        
            
        } else     {  
        
            alert("KO!."); 
            myform.DatesT1.focus(); 
            
        }
      
    }

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's your student-proof solution:
    Code:
    <form action = '#' method = 'post' onsubmit = "return checkForm( this )">
     <input name='data'>
     <input type='submit'>
    </form>
    <script type='text/javascript'>
    
    function checkForm( theForm )
    {
      var result = /[^a-z0-9\,\.\;]/g( theForm.data.value );  
     
      if( result )
      {
        alert('One or more illegal characters were found, the first being character ' + ( result.index + 1 ) + ' "' + result +'".\n\nPlease edit your input.');  
      }
       
      return !result; 
    }
    </script>
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thank u Sir.

    But I have one question for u.

    What difference with this regExp:

    Code:
    /[^a-z0-9\,\.\;]/g,
    and this regExp
    Code:
    "^[a-zA-Z0-9\.;,:' ]{1,100}$"
    ?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Miguel61 View Post
    What difference with this regExp:

    Code:
    /[^a-z0-9\,\.\;]/g,
    and this regExp
    Code:
    "^[a-zA-Z0-9\.;,:' ]{1,100}$"
    One of them has uppercase A-Z as well, and has had added to it colon quote space.
    The numbers in curly braces indicate the min/max characters that will be matched.

    The ^ inside the square brackets indicates that those characters much not match
    Whereas, the ^ at the start, with the $ at the end, indicates that what's in-between must match the entire string.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Miguel61 View Post
    Thank u Sir.

    But I have one question for u.

    What difference with this regExp:

    Code:
    /[^a-z0-9\,\.\;]/g,
    and this regExp
    Code:
    "^[a-zA-Z0-9\.;,:' ]{1,100}$"
    ?
    The first matches any unacceptable characters, the second matches a string of 1-100 acceptable characters.
    Tab-indentation is a crime against humanity.

  6. #6
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thank u Sir.

    But if write this:
    Code:
    var re = [^a-z0-9\,\.\;]/g
    ur script working.

    If write
    Code:
    /^[a-zA-Z0-9\.;,:' ]{1,100}$/g
    ur script not working...

    Why?

    Javascript error: 'index' Is Null or Not an Object
    Line: 29
    Character: 11
    Code: 0
    URI: http://localhost/xml/_checkbox/pagenew.htm

  7. #7
    SitePoint Enthusiast rainner's Avatar
    Join Date
    Apr 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    checking for characters should be easy..

    if( !/[\w\s\;\:\.\,]+/gi.test( WhatEver.Value ) )
    {
    alert( "Check Failed, please, fix it! " );
    return;
    }

    not tested but that would check

    "\w" ( alphanumerics )
    "\s" ( spaces )

    and whatever other characters you want after that.
    make sure to change "WhatEver.Value" with the string to be tested.

  8. #8
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Miguel61 View Post
    Thank u Sir.

    But if write this:
    Code:
    var re = [^a-z0-9\,\.\;]/g
    ur script working.

    If write
    Code:
    /^[a-zA-Z0-9\.;,:' ]{1,100}$/g
    ur script not working...
    I omitted to add the i flag to the expression, but could you show the full code that doesn't work?
    Tab-indentation is a crime against humanity.

  9. #9
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    I omitted to add the i flag to the expression, but could you show the full code that doesn't work?
    Ok (my browser is IE 8):

    Code:
    <form action = '#' method = 'post' onsubmit = "return checkForm( this )">
     <input name='data'>
     <input type='submit'>
    </form>
    
    <script type='text/javascript'>
    
    function checkForm( theForm )
    {
      var result = /^[a-zA-Z0-9\.;,:' ]{1,100}$/g( theForm.data.value );  
     
      if( result )
      {
        alert('One or more illegal characters were found, the first being character ' + ( result.index + 1 ) + ' "' + result +'".\n\nPlease edit your input.');  
      }
       
      return !result; 
    }
    </script>

  10. #10
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your expression represents a required match so the logic is different:
    Code:
    function checkForm( theForm )
    {
      var result = /^[a-z0-9\.;,:'\s]{1,100}$/i( theForm.data.value );  
     
      if( !result )
      {
         alert("No legal characters entered");
      }
       
      return !!result; 
    }
    Tab-indentation is a crime against humanity.

  11. #11
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    Your expression represents a required match so the logic is different:
    Code:
    function checkForm( theForm )
    {
      var result = /^[a-z0-9\.;,:'\s]{1,100}$/i( theForm.data.value );  
     
      if( !result )
      {
         alert("No legal characters entered");
      }
       
      return !!result; 
    }
    Sorry Sir, but ur code not working...

    PHP Code:
    <form action '#' method 'post' onsubmit "return checkForm( this )">
     <
    input name='data'>
     <
    input type='submit'>
    </
    form>
     
    <
    script type='text/javascript'
     
    function 
    checkFormtheForm )
    {
      var 
    result = /^[a-z0-9\.;,:'\s]{1,100}$/i( theForm.data.value );  
     
      if( !result )
      {
         alert("No legal characters entered");
      }
       
      return !!result; 
    }
    </script> 

  12. #12
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    Your expression represents a required match so the logic is different:
    Code:
    function checkForm( theForm )
    {
      var result = /^[a-z0-9\.;,:'\s]{1,100}$/i( theForm.data.value );  
     
      if( !result )
      {
         alert("No legal characters entered");
      }
       
      return !!result; 
    }
    Sorry Sir, but ur code not working... are u tested?

    PHP Code:
    <form action '#' method 'post' onsubmit "return checkForm( this )">
     <
    input name='data'>
     <
    input type='submit'>
    </
    form>
     
    <
    script type='text/javascript'
     
    function 
    checkFormtheForm )
    {
      var 
    result = /^[a-z0-9\.;,:'\s]{1,100}$/i( theForm.data.value );  
     
      if( !result )
      {
         alert("No legal characters entered");
      }
       
      return !!result; 
    }
    </script> 

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Miguel61 View Post
    Sorry Sir, but ur code not working...
    I have run up a test of the code that was posted and it seems to work. What seems to be the problem?

    Do you have any test inputs that are not working as expected?
    Or is something more serious occurring. If so, something might be conflicting that we can resolve by taking a look.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    What seems to be the problem?
    The form is validate even with invalid characters in the input name='data'.

  15. #15
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Miguel61 View Post
    The form is validate even with invalid characters in the input name='data'.
    What characters do you consider to be invalid?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  16. #16
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    What characters do you consider to be invalid?
    All characters differents by letters, numbers and ' , ; . : and the backspace.

    For example ? is invalid, ^ is invalid, / is invalid, * is invalid, etc.

  17. #17
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Miguel61 View Post
    All characters differents by letters, numbers and ' , ; . : and the backspace.

    For example ? is invalid, ^ is invalid, / is invalid, * is invalid, etc.
    It seems to work on the test code that I created from your post, so assuming that your test code is the same, there must be something else causing the problem.

    Can you help me to take a look at your test page? Commonly the most effective way is for you to link to a test page that you have on the internet, or failing that, you can attach a file to a post.

    By the way. The script will only run when scripting is actually enabled.

    If you're testing a local page on IE, be sure to enable scripting. IE doesn't enable scripting on local web pages until you choose to enable it from the yellow info bar.
    See for an example: http://www.troublefixers.com/disable...ng-animations/
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  18. #18
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    It seems to work on the test code that I created from your post, so assuming that your test code is the same, there must be something else causing the problem.

    Can you help me to take a look at your test page? Commonly the most effective way is for you to link to a test page that you have on the internet, or failing that, you can attach a file to a post.

    By the way. The script will only run when scripting is actually enabled.

    If you're testing a local page on IE, be sure to enable scripting. IE doesn't enable scripting on local web pages until you choose to enable it from the yellow info bar.
    See for an example: ActiveX Warning in Internet Explorer | Internet Explorer has restricted this webpage from running scripts or ActiveX controls

    Dear friend, try it yourself:
    http://www.avia-it.com/testaruba/_check.htm

    This code not working...

  19. #19
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Miguel61 View Post
    Dear friend, try it yourself:
    http://www.avia-it.com/testaruba/_check.htm

    This code not working...
    What do you mean by "not working"

    • Does the form submit to a blank page?
    • Does the form submit to an error page?
    • Does the alert appear when it shouldn't?
    • Does your computer explode at random?


    Can you please break this down in to steps that I can follow to try and duplicate your experience. For example:
    • what you enter
    • what you expect to occur
    • the problem that actually occurs


    From what I'm getting from you right now, I believe that you do not have scripting active on your web browser for this page.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  20. #20
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thanks for ur help.

    I confirm: this script not working and all javascript in my computer work properly without problems.

    The form validate even with invalid characters in the input name='data'.

    | = validate
    \ = validate
    ! = validate
    = validate
    $ = validate
    % = validate
    & = validate
    / = validate
    ( = validate
    ) = validate
    = = validate
    ? = validate
    ^ = validate
    * = validate
    [ = validate
    ] = validate
    + = validate
    - = validate
    _ = validate
    @ = validate
    # = validate
    = validate
    = validate
    = validate

    But it's not important, I can find other solution for my problem.

    Regards and thanks for ur time.

  21. #21
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Miguel61 View Post
    I confirm: this script not working and all javascript in my computer work properly without problems.

    The form validate even with invalid characters in the input name='data'.
    Are you using IE, Firefox, Chrome, Opera, Safari?

    If IE, have you enabled the advanced notification about script errors? I ask because any kind of script failure fails to prevent the form from being submitted.

    I normally test in Chrome, but when I use IE to test your page with that setting enabled, I do see an error occur. I'll investigate further and find out what is causing the problem.

    Quote Originally Posted by Miguel61 View Post
    But it's not important, I can find other solution for my problem.

    Regards and thanks for ur time.
    Oh, I see. Okay.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  22. #22
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The problem that you're facing with IE is that IE is dumber than your average browser.
    The regular expression needs to use the exec() method to pass the string.

    Code:
    var result = /^[a-z0-9\.;,:''\s]{1,100}$/i.exec( theForm.data.value );
    So this code allows IE to work properly.

    Code javascript:
    var result = /^[a-z0-9\.;,:''\s]{1,100}$/i.exec( theForm.data.value );
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  23. #23
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The problem that you're facing with IE is that IE is dumber than your average browser.
    The regular expression needs to use the exec() method to pass the string.

    Code:
    var result = /^[a-z0-9\.;,:''\s]{1,100}$/i.exec( theForm.data.value );
    So this code allows IE to work properly.

    Code javascript:
    var result = /^[a-z0-9\.;,:''\s]{1,100}$/i.exec( theForm.data.value );
    Thanks Sir, now this code working.

  24. #24
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Miguel61 View Post
    Dear friend, try it yourself:
    http://www.avia-it.com/testaruba/_check.htm

    This code not working...
    Code:
    I.E. won't execute the regex directly, so exec must be called...
    
    The regex you're using cannot differentiate between bad input and no input,
    which dictates testing to ensure that the string has a length.
    
    <script type='text/javascript'>
    
    function checkForm( theForm )
    {
      var result = /^[a-z0-9\.;,:'\s]{1,100}$/i.exec( theForm.data.value );  
     
      if( !result )
      {
         alert( theForm.data.value.length ? "Illegal characters entered" : "No legal characters entered");
      }
       
      return !!result; 
    }
    </script>
    Tab-indentation is a crime against humanity.

  25. #25
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    402
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thanks Sir!


Bookmarks

Posting Permissions

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