How to Restrict Special Characters in Uitextfield Except Dot and Underscores

How can I restrict special characters in UITextField except dot and underscores?

Try code block given below, it worked fine for me.

SWIFT 3.0

let ACCEPTABLE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let cs = NSCharacterSet(charactersIn: ACCEPTABLE_CHARACTERS).inverted
let filtered = string.components(separatedBy: cs).joined(separator: "")

return (string == filtered)
}

Objective C

#define ACCEPTABLE_CHARACTERS @" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARACTERS] invertedSet];

NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

return [string isEqualToString:filtered];
}

Hope it will work for you as well.

How to limit length and special characters of UItextField?

Alternative to regex, you can first get all the allowed characters into a set:

 var charactesAllowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."
var charactersSet = [Character](charactesAllowed)

then try to see if the most recently typed charactes is in this array

var newCharacter = //whatever the character is, ex: "A"
if(contains(charactersSet, newCharacter))
{
println("Allowed")
// Add it into the label text
}

How to avoid special characters (@, #, &, etc.) in UITextfield validation

Assume #$% are the special characters you want.
You want at least one special character, with the possability of mixing it with alphanumerical characters.

Your regular expression would then be something like:
([A-Z0-9a-z]*(#|$|%)+[A-Z0-9a-z]*)+

How to disallow special characters but allow Asian/Middle Eastern characters in a UITextField

Use the solution from the other question but instead of building the character set from the fixed letters, use the standard NSCharacterSet alphanumericCharacterSet.

UITextfield limit with no special charecters ios objective c

Check the length. If it's too long, return NO. Otherwise check if the characters are valid.

Your code for checking for valid characters is very inefficient. Splitting and rejoining the whole string is not a good way to check to see if it contains any invalid characters.

The following code is a better way to handle your case:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = textField.text.length + string.length - range.length;
if (newLength > limitPassportNumber) {
return NO; // too long
}

NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARACTERS] invertedSet];
NSRange badRange = [string rangeOfCharacterFromSet:cs];

return badRange.location == NSNotFound;
}

How To Restrict A Username Textfield


func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let s = NSString(string: textField.text ?? "").replacingCharacters(in: range, with: string)
guard !s.isEmpty else { return true }
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .none
return numberFormatter.number(from: s)?.intValue != nil
}


Related Topics



Leave a reply



Submit