1

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 Regular Expressions from the jQuery code: 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

I searched for a complete solution and probably missed it, so I decided to pick the pieces up and implement NSString+JavaScript+RegEx...

I've posted it as an answer below, in case it's of value to anyone...

Reuven

2 Answers 2

2

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

// // 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]\\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]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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

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried the following UIWebView instance method?

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

4 Comments

Yes, I have. In my class below (that I posted as an answer to my own question, hoping it will be useful for others...) I'm using it.
Well then obviously this isn't a question and shouldn't be posted on this website!!
I'm sorry for possibly misusing/abusing the etiquette of the site. I was looking in this site for the answer, and couldn't find one, leading me to write one myself; And since I couldn't find the answer on the site, I assumed others may be looking for a complete answer, and hoping my answer will be of value...
I suggest deleting the question, seeing as it isn't a question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.