Skip to content Skip to sidebar Skip to footer

Iphone - Evaluating Javascript And Regular Expressions Using Uiwebview

In my iPhone project, I needed to be able to validate a string either by using a Javascript function or by using a regular expression. This code was tested using the JavaScript and

Solution 1:

Here are the code listings, hope you find is useful:

// // NSString+JavaScript+RegEx.h //

@interfaceNSString(JavaScript_RegEx)// Built-in rules
+ (NSArray*) emailRule;
+ (NSArray*) urlRule;
+ (NSArray*) dateRule;
+ (NSArray*) dateISORule;
+ (NSArray*) numberRule;
+ (NSArray*) digitsRule;
+ (NSArray*) creditcardRule;

- (NSString*) javascriptValue;
- (NSString*) regexTestValue:(NSString*)value;

@end

// // NSString+JavaScript+RegEx.m //

#import "NSString+JavaScript+RegEx.h"

@implementationNSString(JavaScript_RegEx)

staticNSString* kUIWebViewWrapperForJS = @"(function () {\ntry {\n%@\n}\ncatch (err)\n{\nreturn err;\n}\n}) ();";
staticNSString* kJSWrapperForRegEx = @"re = new RegExp(/%@/); return re.test('%@')";

+ (NSArray*) emailRule {
    return [NSArray arrayWithObjects:[NSNumber numberWithInt:0], // 0=RegEx
            @"^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?$",
            @"Please enter a valid email address.",
            nil];
}

+ (NSArray*) urlRule {
    return [NSArray arrayWithObjects:[NSNumber numberWithInt:0], // 0=RegEx
            @"^(https?|ftp):\\/\\/(((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:)*@)?(((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]))|((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?)(:\\d*)?)(\\/((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)+(\\/(([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)*)*)?)?(\\?((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)|[\\uE000-\\uF8FF]|\\/|\\?)*)?(\\#((([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(%[\\da-f]{2})|[!\\$&'\\(\\)\\*\\+,;=]|:|@)|\\/|\\?)*)?$",
            @"Please enter a valid URL.",
            nil];
}

+ (NSArray*) dateRule {
    return [NSArray arrayWithObjects:[NSNumber numberWithInt:1], // 1=JS
            @"return !/Invalid|NaN/.test(new Date(\"%@\"));", // expect a date
            @"Please enter a valid date.",
            nil];
}

+ (NSArray*) dateISORule {
    return [NSArray arrayWithObjects:[NSNumber numberWithInt:0], // 0=RegEx
            @"^\\d{4}[\\/-]\\d{1,2}[\\/-]\\d{1,2}$",
            @"Please enter a valid date.",
            nil];
}

+ (NSArray*) numberRule {
    return [NSArray arrayWithObjects:[NSNumber numberWithInt:0], // 0=RegEx
            @"^-?(?:\\d+|\\d{1,3}(?:,\\d{3})+)(?:\\.\\d+)?$",
            @"Please enter a valid number.",
            nil];
}

+ (NSArray*) digitsRule {
    return [NSArray arrayWithObjects:[NSNumber numberWithInt:0], // 0=RegEx
            @"^\\d+$",
            @"Please enter only digits.",
            nil];
}

+ (NSArray*) creditcardRule {
    return [NSArray arrayWithObjects:[NSNumber numberWithInt:1], // 1=JS
            @"// accept only digits and dashes\n""var value = \"%@\";\n""if (/[^0-9-]+/.test(value))\n""   return false;\n""var nCheck = 0, nDigit = 0, bEven = false;\n""value = value.replace(/\\D/g, \"\");\n""for (var n = value.length - 1; n >= 0; n--) {\n""   var cDigit = value.charAt(n);\n""   var nDigit = parseInt(cDigit, 10);\n""   if (bEven) {\n""   if ((nDigit *= 2) > 9)\n""       nDigit -= 9;\n""   }\n""   nCheck += nDigit;\n""   bEven = !bEven;\n""}\n""return (nCheck %% 10) == 0;",
            @"Please enter a valid credit card number.",
            nil];
}

- (NSString*) javascriptValue {
    NSString* jsForWebView = [NSString stringWithFormat:kUIWebViewWrapperForJS, self]; // convert to "JS-edible-by-UIWebView" UIWebView*wb = [[[UIWebView alloc] init] autorelease];
    //NSLog(@"%s\n%@\n", __FUNCTION__, jsForWebView);NSString* result = [wb stringByEvaluatingJavaScriptFromString:jsForWebView];
    return result;
}

- (NSString*) regexTestValue:(NSString*)value {
    NSString* javascriptWrappedRegEx = [NSString stringWithFormat:kJSWrapperForRegEx, self, value];
    NSString* result = [javascriptWrappedRegEx javascriptValue];
    return result;
}

@end

Enjoy, Reuven

Solution 2:

Have you tried the following UIWebView instance method?

- (NSString *) stringByEvaluatingJavaScriptFromString: (NSString *) script

Post a Comment for "Iphone - Evaluating Javascript And Regular Expressions Using Uiwebview"