I have create username and create password. There is no character limit for username but password is has 8 digit character limit. How to disable the copy/paste only for password field. And also how to filter some characters which are not valid for username.
Asked
Active
Viewed 7,234 times
-2
-
1FYI, this question has already been answered. Please see: https://stackoverflow.com/questions/29596043/how-to-disable-pasting-in-a-textfield-in-swift – BlondeSwan Nov 05 '18 at 20:31
-
@TannerJuby No this question is not answered. The link which you shared is generic solution to disable copy and paste, but the one which i posted is for how to disable copy paste for specfic field and how to filter the characters. – moksha harish Nov 05 '18 at 21:26
-
@mokshaharish Yes, I understand. I posted an answer below which answers your question. And the "generic solution" that was shared will work for a specific field (i.e. the field that you assign the custom class to). If you still don't think it's specific enough (i.e. you would want to use the custom TextField for many different fields) then I suggest creating a boolean such as `pasteable` as a variable in the custom class and setting that to true/false upon initialization of the field. And then use that boolean to disable posting via `canPerformAction` – BlondeSwan Nov 05 '18 at 21:56
-
@ksav Why is that? – BlondeSwan Nov 06 '18 at 16:58
-
@TannerJuby https://ux.stackexchange.com/questions/21062/preventing-a-user-from-pasting-from-the-clipboard-into-a-mandatory-form-field – ksav Nov 06 '18 at 17:10
-
@ksav I would say let the UX designer of the product decide that as there are certainly cases in which you don't want to allow pasting in a field. – BlondeSwan Nov 07 '18 at 17:30
-
@TannerJuby if you're at the point where you are even considering this feature then chances are that your 'UX designer' already resigned. – ksav Nov 07 '18 at 17:56
-
@ksav There are plenty of large scale, very successful, great UX products that I've encountered that don't allow user's to paste a password, financial account number, or other fraud preventative inputs. Don't tell people they can't do something just because your opinion conflicts it. – BlondeSwan Nov 07 '18 at 22:02
1 Answers
0
For the Copy and Paste Question:
You will need to create your own TextField class that inherits UITextField and then override the canPerformAction
delegate method to disable pasting:
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == "paste:" {
return false
}
return super.canPerformAction(action, withSender: sender)
}
More detailed description: How to disable Pasting in a TextField in Swift?
For the Characters Question:
Similar concept as above, except you will need to implement the textFieldDidEndEditing
or shouldChangeCharactersIn
delegate methods.
textFieldDidEndEditing
will allow you to evaluate the string in the field AFTER the user has stopped editing that field. Example:
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
switch textField {
case usernameTextField:
// evaluate the `textField.text` for is a valid username
case passwordTextField:
// evaluate the `textField.text ` for is a valid password
}
}
shouldChangeCharactersIn
will allow you to evaluate the string for every keystroke (I prefer using the later when evaluating textfield inputs). Example:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
switch textField {
case usernameTextField:
// evaluate the `replacementString` for is a valid username
case passwordTextField:
// evaluate the `replacementString` for is a valid password
}
return true
}

BlondeSwan
- 772
- 5
- 26
-
2Maybe credit the original https://stackoverflow.com/a/29596354/2907715 – ielyamani Nov 05 '18 at 20:35
-
-
@Carpsen90 I also don't necessarily consider this a duplicate because there was also a question in there about filtering characters. I consider this question to be about UITextField evaluation/modification in a general sense rather than specifically that single Copy and Paste question. – BlondeSwan Nov 05 '18 at 20:57
-
this doesn't answer how to prevent pasting for specific fields tho. does `sender` have access to the textfield? – frlzjosh Sep 23 '22 at 21:15
-
1@frlzjosh yes it does, the `if action === "paste:" return false` does exactly that – BlondeSwan Sep 26 '22 at 20:21