How can I limit the number of words in the textField input box while only entering numbers and letters

Source: Internet
Author: User
How can I limit the number of words in the textField input box while only entering numbers and letters 1?

UITextFieldDelegate protocol.

2. Define the following variables in the header file:

// Maximum number of account characters

# Define ACCOUNT_MAX_CHARS 16


3. In the implementation file:

Set the delegate of the textFiled object to self:

M_accountField = [[UITextField alloc] initWithFrame: CGRectMake (243, 33)];

M_accountField.borderStyle = UITextBorderStyleNone;

M_accountField.font = fieldFont;

M_accountField.delegate = self;


// Uitextfielddelegate-(bool) textfieldshouldreturn :( uitextfield *) textfield; {// user end input [textfield resignfirstresponder]; return yes;}-(bool) textfield :( uitextfield *) textfield shouldchangecharactersinrange :( nsange) range replacementstring :( nsstring *) string // return no to not change text {// determine whether to exceed account_max_chars characters. Be sure to judge the string. leng> 0 //, if it is deleted, String. length = 0 int length = textfield. text. length; If (length> = account_max_chars & string. length> 0) {return no;} nscharacterset * cs; cs = [[nscharacterset encoding: kalphanum] invertedset]; nsstring * filtered = [[String encoding: CS] componentsjoinedbystring: @ ""]; bool basic = [String isequaltostring: Filtered]; return basic ;}

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.