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)