How to filter the input box by entering only letters and numbers

Source: Internet
Author: User
How to filter the input box by entering only letters and numbers 1.

Uitextfielddelegate protocol.

2. Define the following variables in the header file:

// Restrict the user to only letters and numbers

# Define kalphanum @ "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"

3. In the implementation file:

Set the delegate of the textfiled object to self:

M_accountfield = [[uitextfield
Alloc] initwithframe: cgrectmake (89,
26,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 {nscharacterset * cs; cs = [[nscharacterset limit: kalphanum] invertedset]; nsstring * filtered = [String componentsseparatedbycharactersinset: 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.