SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression Help..

    Hi. I'm using the following function to check input on text fields for invalid characters (mostly foreign characters). It works if there is only invalid characters in the string, and it works if there are valid characters typed before the invalid characters, but doesn't work if there are valid characters typed before and after the invalid ones. Can someone tell me what i'm doing wrong? Thanks!

    Code:
    function chkchar2()
    {
    
    	for (var j=0;j < document.forms[0].length;j++) 
    	{
    		if (document.forms[0].elements[j].type == "text" || document.forms[0].elements[j].type == "textarea")
    		{ 
    			for (var i = 0; i < document.forms[0].elements[j].value.length; i++)
    			{
    				var myregexp = new RegExp("[A-Z]|[a-z]|\\s|[_]|[0-9]|[.]|[']|[\"]|[!]|[@]|[#]|[$]|[%]|[&]|[*]|[(]|[)]|[-]|[=]|[+]|[~]|[;]|[:]|[<]|[>]|[.]|[?]|[/]|[|]|[\\]|[{]|[}]|[\\^]");
    for (var x = 0; x < document.forms[0].elements[j].value.length; x++)
    {			
    var match = myregexp.exec(document.forms[0].elements[j].value.substring(x));
    
    
    				if (match != null) {
    			
    	
    			} else {alert("invalid");
    					j++;
    
    }
    
    
    				}
    			}
    		}
    	}
    }

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why are you worrying whether they are before or after or in the middle? All you need to do is check if they are there. Try this one:
    Code:
    [^A-Za-z0-9\s_\.'\"!@#$&#37;&*()-=+~;:<>.?/|{}\^]+
    Those there are the allowed characters, I suppose. The expression will match anything that isn't one of those.


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
  •