SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Can anyone help me convert this ASP RegExp to Javascript?

    Currenty, I am using this ASP RegExp to identify any invalid characters a user may submit in a form.
    Code:
    REM - Retrieve invlalid character 
    Function IsolateInvalid(strInput, strPattern) 
      Dim regEx, Match, Matches, Position, strReturn 
      Position = 1 
      strReturn = "" 
     
      Set regEx = New RegExp 
    	  regEx.Pattern = strPattern   
    	  regEx.IgnoreCase = True  
    	  regEx.Global = True 
     
      Set Matches = regEx.Execute(strInput) 
     
      For Each Match in Matches 
      ' Below line adds everthing from the current position to the next pattern match 
    	  strReturn = strReturn & Mid(strInput, Position, Match.FirstIndex+1-Position) 
     
    	  Position = Len(Match.Value) + Match.FirstIndex + 1 
      Next 
      
      ' Add any text after the last match 
      strReturn = strReturn & Mid(strInput, Position, Len(strInput)) 
     
      IsolateInvalid = strReturn 
    End Function 
     
    IsolateInvalid(user_name, "[a-zA-Z_0-9\s\,\.\&\-\xd3\xe1\xf2\xf1]+")
    If a user accidently types in an invalid character in their "user_name", the RegExp identifies the invalid character and displays it. Something like: "Invalid character(s) @ found in your username".

    Can anyone help me convert this so it works in javascript as well?
    Thanks!
    Keomed.com Webmaster
    www.keomed.com

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A basic example
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>Validation</title>
    <meta name="Generator" content="TextPad 4.4" />
    <meta name="Author" content="Peter Bailey" />
    <script type="text/javascript">
    	function validateForm( f )
    	{
    		var elem = f.elements['user_name'];
    		var ivChars = elem.value.match( /[^\w\d\s\,\.\&\-\xd3\xe1\xf2\xf1]/gi );
    		if ( ivChars != null )
    		{
    			alert( 'Some characters you have entered are invalid. They are listed below\n\n' + ivChars.join(' ') );
    			return false;
    		}
    		return true;
    	}
    </script>
    </head>
    <body>
    <form onsubmit="return validateForm( this )">
    	<input type="text" name="user_name" />
    	<input type="submit" name="Submit" value="Submit" />
    </form>
    </body>
    </html>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Addict
    Join Date
    Feb 2000
    Location
    Minnesota
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Beetle, it worked perfectly!
    Thanks for your help!
    Keomed.com Webmaster
    www.keomed.com


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
  •