how to validate a website in a textfiled?

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

  1. rethish macrumors member

    Joined:
    Oct 16, 2008
    #1
    -(IBAction)web:(id)sender
    {

    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];

    }

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

    }
    }



    when this code is used www.hai.com , wwwww.fff.com , wwwfghw.hai.com , 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: www.hhhh.com , the w should appear only three times otherwise it must not be valid.


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

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    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 "wwwfghw.hai.com" 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