function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
disturbed118disturbed118 

Help with apex regEx for alpha numeric pattern

I am trying to validate a string as alpha numeric only i have a method

 

public static Boolean IsAlphaNumeric(string field)
    {
        if(!IsObjectNull(field))
            return (Pattern.matches('^[a-zA-Z0-9]+$', field));
        return false;
    }

 

 

but it always returns true even if the string is aaa or 111? how do i say only aaa111

SteveBowerSteveBower

Well 'aaa' *is* an alphanumeric, it just happens to have no numbers in it.  So, what exactly are you trying to match?  If it's a string with at least one alpha, and at least one digit after it, then you want:

 

'^[a-zA-Z]+\d+$'

 

There are many many sites out there with excellent tutorials, etc. on Regex, youjust have to crystal clear on what you're trying to accept and what you won't accept.

 

Best, Steve.