SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex in JS not working, but working in PHP

    I've got a preg_match in PHP that is working. It attempts to check a value for non-US keyboard characters:

    Code PHP:
    if( preg_match( '/[^\x20-\x3B\x3D\x3F-\x7E\s]/', $value ) )
    {
      // ...
    }

    So, this is working. If I put foreign chars in $value, it definitely finds a match. JS on the other hand, not working with the same regex:

    Code JavaScript:
    var regex = /[^\x20-\x3B\x3D\x3F-\x7E\s]/;
    if( this.value.search(regex) != '-1' ){
      // ...
    }

    Since I don't read or speak any foreign languages, I use charset restriction to keep spam from going through my forms, but I want to detect it client side if possible. Any help is appreciated.

  2. #2
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It appears that because of the way I was using preg_match vs replace, I needed my regex in JS to include a g modifier...


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
  •