iPhone - 使用UIWebView评估JavaScript和正则表达式

时间:2011-02-03 19:29:11

标签: javascript iphone regex

在我的iPhone项目中,我需要能够通过使用Javascript函数或使用正则表达式来验证字符串。

此代码使用jQuery代码中的JavaScript和正则表达式进行了测试: http://docs.jquery.com/Plugins/Validation/Methods/email http://docs.jquery.com/Plugins/Validation/Methods/url http://docs.jquery.com/Plugins/Validation/Methods/date http://docs.jquery.com/Plugins/Validation/Methods/dateISO http://docs.jquery.com/Plugins/Validation/Methods/number http://docs.jquery.com/Plugins/Validation/Methods/digits http://docs.jquery.com/Plugins/Validation/Methods/creditcard

我搜索了一个完整的解决方案,可能错过了,所以我决定选择这些部分并实现NSString + JavaScript + RegEx ......

我在下面发布了一个答案,以防它对任何人都有价值......

鲁文

2 个答案:

答案 0 :(得分:2)

以下是代码清单,希望您发现有用:

<强> // // NSString + JavaScript + RegEx.h //

@interface NSString(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"

@implementation NSString(JavaScript_RegEx)

static NSString* kUIWebViewWrapperForJS = @"(function () {\ntry {\n%@\n}\ncatch (err)\n{\nreturn err;\n}\n}) ();";
static NSString* 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]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CF\\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

享受,Reuven

答案 1 :(得分:0)

您是否尝试过以下UIWebView实例方法?

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