Help with some code conversion

I am trying to validate a text field for an email address. I found this Objective C code that will validate a NSString to make sure it is a proper email address.

  • (BOOL)validateEmail:(NSString *)email {
    NSString *emailRegex = @“[A-Z0-9a-z._%±]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}”;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@“SELF MATCHES %@”, emailRegex];
    return [emailTest evaluateWithObject:email];
    }

How would I either:

a. Convert this to Applescript.

b. Leave it as Objective C and call it from my Applescript code.

Assume the string I will be handing off is a simple Applescript string.

Thanks.

Here’s a translation:

on validateEmail_(emailAddress)
	set emailRegex to "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
	set emailTest to current application's NSPredicate's predicateWithFormat_("SELF MATCHES %@", emailRegex)
	return emailTest's evaluateWithObject_(emailAddress)
end validateEmail_

Note the escaping of the backslash.

Now we wait for the inevitable posts about how the regular expression is insufficient :slight_smile: