# Check textfield in alertview

**URL:** <https://forums.kodeco.com/t/check-textfield-in-alertview/36208>\
**Category:** Swift\
**Created:** [November 3, 2017, 10:49am UTC](https://forums.kodeco.com/t/check-textfield-in-alertview/36208 "2017-11-03T10:49:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joachimfh](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@joachimfh](https://forums.kodeco.com/u/joachimfh)\
**Post date:** [November 3, 2017, 10:49am UTC](https://forums.kodeco.com/t/check-textfield-in-alertview/36208/1 "2017-11-03T10:49:13Z")

</div>

I am more or less new to iOS and there is a lot of fundamental stuff I am missing. I am following your HitList tutorial, and I would like to check wheter my textfield in the alertview is empty or not. So if the field is empty the save-button should be dissabled. How do I do that?

thanks,  
Joachim

---

<div class="post-metadata">

**Author:** ![gargoyle](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gargoyle/32/107285_2.png) [@gargoyle](https://forums.kodeco.com/u/gargoyle)\
**Post date:** [November 5, 2017, 5:25am UTC](https://forums.kodeco.com/t/check-textfield-in-alertview/36208/2 "2017-11-05T05:25:51Z")

</div>

You would need to start off with it being disabled. Mark your UITextField’s delegate to be the view controller, and implement this method:

```
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let oldText = textField.text!
    let swiftRange = Range(range, in: oldText)!
    let newString = oldText.replacingCharacters(in: swiftRange, with: string)

    saveButton.enabled = !newString.isEmpty

    return true;

```

---

<div class="post-metadata">

**Author:** ![gargoyle](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gargoyle/32/107285_2.png) [@gargoyle](https://forums.kodeco.com/u/gargoyle)\
**Post date:** [November 5, 2017, 5:28am UTC](https://forums.kodeco.com/t/check-textfield-in-alertview/36208/3 "2017-11-05T05:28:24Z")

</div>

Oh sorry, you said in an alert view, which is different. See here: [this post](https://stackoverflow.com/questions/30596851/how-do-i-validate-textfields-in-an-uialertcontroller)

---

<div class="post-metadata">

**Author:** ![ofiron](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ofiron/32/102075_2.png) [@ofiron](https://forums.kodeco.com/u/ofiron)\
**Post date:** [November 5, 2017, 10:55am UTC](https://forums.kodeco.com/t/check-textfield-in-alertview/36208/4 "2017-11-05T10:55:48Z")

</div>

This code in Objective-C but it should be the same idea in swift

```nohighlight
NSString *text = [[alertView textFieldAtIndex:0] text];

```

edited:  
Sorry @gargoyle didn’t see your link

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [April 19, 2018, 3:00am UTC](https://forums.kodeco.com/t/check-textfield-in-alertview/36208/5 "2018-04-19T03:00:39Z")

</div>

Closed on April 19, 2018, 3:00am UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
