how to validate a website in a textfiled?

Discussion in 'Mac Programming' started by rethish, Nov 18, 2008.

  1. rethish macrumors member

    Oct 16, 2008

    NSString *web =[[NSString alloc]initWithString:[webfield stringValue]];

    NSString *webRegEx = @"www.[A-Z0-9a-z._%+-]+.[a-z]{2,4}";

    NSPredicate *webTest = [NSPredicate predicateWithFormat:mad:"SELF MATCHES %@",webRegEx];

    if ([webTest evaluateWithObject:web] == YES)

    [NSApp beginModalSessionForWindow:valid];
    [webfield web:sender];


    [NSApp beginModalSessionForWindow:notvalid];
    [webfield setStringValue:mad:""];
    [webfield web:sender];


    when this code is used , , , these format are shown valid and also the letter 'w' can used any number of times and also other letters are also accepted .

    I need a standard format ie: , the w should appear only three times otherwise it must not be valid.

    How can it be poosible ? please help
  2. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    The problem is, a site may not have a 'www' and still be valid, or it may have www1, www2, etc. so building your own check will be a mess of regexes (and it's re-inventing the wheel); even your example of "" is perfectly valid. The say the easiest thing to do would be using the NSURL class to check the format. the +(id)URLWithString: method will return nil if you pass it a malformed url and non-nil if the passed string is a correctly formed url. This way you could then use the newly created class to extract out the specific information you need.

Share This Page