IOS Digital Formatting (mobile phone number, bank card number format, verification code input control)

Source: Internet
Author: User

Demand mobile number 15288888888------->152 8888 8888 Bank card number 8888888888888888888----->8888 8888 8888 8888 888

Implementation of number formatting at uitextfiled input

In the Uitextfield proxy method Shouldchangecharactersinrange implementation, you can use the classification can be used globally, convenient and fast

NSString *text = [TextFieldtext];

// only numbers can be entered

nscharacterset *characterset = [nscharactersetcharactersetwithcharactersinstring:@ " 0123456789\b "];

string = [string stringbyreplacingoccurrencesofstring:@ ""withstring:@ ""];

if ([stringrangeofcharacterfromset: [CharacterSetInvertedset]]. Location! =nsnotfound)

{

returnNO;

}

Text = [Text stringbyreplacingcharactersinrange: Rangewithstring: string];

Text = [Text stringbyreplacingoccurrencesofstring:@ ""withstring:@ ""];

// If the phone number is formatted, you need to add these three lines of code if you're judging a bank card, just take these three comments.

nsmutablestring *temstring = [nsmutablestringstringwithstring: text];

[Temstring insertstring:@ ""atindex:0];

Text = temstring;

nsstring *newstring =@ "";

while (text. Length >0)

{

nsstring *substring = [textsubstringtoindex:MIN(text.length,4)];

newstring = [newstring stringbyappendingstring: subString];

if (subString. Length = =4)

{

newstring = [newstring stringbyappendingstring:@ ""];

}

Text = [Text substringfromindex:MIN(text.length,4)];

}

newstring = [newstring stringbytrimmingcharactersinset: [CharacterSetInvertedset]];

// number + bank card

if (newstring. Length >=)

{

returnNO;

}

[TextField setText: newstring];

returnNO;

Here refer to http://blog.csdn.net/rlpeng/article/details/21292059 This article, thank you for sharing I wrote a classification, can be directly copied using

Demo Address: Post-update ·

IOS Digital Formatting (mobile phone number, bank card number format, verification code input control)

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.