iOS settings TextField can only enter numbers for phone numbers

Source: Internet
Author: User

first set Uitextfield's keyboard to number Pad in. xib, but the keyboard will be cut back to other keyboards when used to verify the content. Through the magic of Baidu I know the following methods to solve the problem:

First let. Xib's Viewcontroller implement Uitextfielddelegate and then associate with the control you want to validate.

The above is xib, if the code is set, do not consider or the part of the face

Then paste the following code into the class.

-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string {    return [self validatenumber:string];} -(BOOL) ValidateNumber: (nsstring*) Number {    BOOL res = YES;    nscharacterset* Tmpset = [Nscharacterset charactersetwithcharactersinstring:@ "0123456789"];    int i = 0;    while (I < number.length) {        NSString * string = [number Substringwithrange:nsmakerange (I, 1)];        Nsrange range = [string Rangeofcharacterfromset:tmpset];        if (Range.length = = 0) {            res = NO;            break;        }        i++;    }    

There is another way, I haven't tried this way, but I found it and shared it:

1. Define constants for use

#define NUMBERS @ "0123456789"

2. Do the following

-(BOOL) TextField: (uitextfield*) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( nsstring*) string{        Nscharacterset*cs;        CS = [[nscharactersetcharactersetwithcharactersinstring:numbers] invertedset];        nsstring*filtered = [[string Componentsseparatedbycharactersinset:cs] componentsjoinedbystring:@ ""];        Boolbasictest = [string isequaltostring:filtered];        if (!basictest) {            uialertview* alert = [[Uialertviewalloc] initwithtitle:@ "Prompt"                                                            message:@ "Please enter a number"                                                           delegate : nil                                                  cancelbuttontitle:@ "OK"                                                  Otherbuttontitles:nil];            [Alert show];            Returnno;        }    Returnyes;}

iOS settings TextField can only enter numbers for phone numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.