How to Check Text Field Input at Real Time

How to check text field input at real time?

Using -textField:shouldChangeCharactersInRange:replacementString: is probably a bad solution because it fires before the text field updates. This method should probably be used when you want to change the text of the text field before the keyboard automatically updates it. Here, you probably want to simply use target-action pairing when editing value changes:

[textField addTarget:self action:@selector(checkTextField:) forControlEvents:UIControlEventEditingChanged];

Then, in - (void)checkTextField:(id)sender, try this:

UITextField *textField = (UITextField *)sender;
if ([textField.text length] == 8) {
textField.textColor = [UIColor greenColor]; // No cargo-culting please, this color is very ugly...
} else {
textField.textColor = [UIColor blackColor];
/* Must be done in case the user deletes a key after adding 8 digits,
or adds a ninth digit */
}

How to check contents of input in real time

You can use the input event to get changes when the user is typing inside of the field. On the change event you will be given the element corresponding to the input element the event listener is attached to as event.target. Using this reference you are able to get the value of the input by using the value property on the element.

After you have retrieved the value you will need to verify that it is in fact numerical. Thankfully jQuery provides a method called isNumeric that allows us to do just that.

Once we've established the fact that the value is indeed numerical you can either apply a class or just set the style to what you want to be. Make sure you also check if the value has been emptied so the user does not get confused.

As for the validation message - it's not a good idea to set the value of the input as the user is interacting with said element. Instead I've chosen to add textual element to represent the message that will be shown conditionally based on the validation state.

// Add error message element after input.$('#some-number').after('<span class="error-message">Please enter numbers only!</span>')
$('#some-number').on('input', function (evt) { var value = evt.target.value if (value.length === 0) { evt.target.className = '' return }
if ($.isNumeric(value)) { evt.target.className = 'valid' } else { evt.target.className = 'invalid' }})
input {  -webkit-appearance: none;  -moz-appearance: none;  appearance: none;  outline: none;  border: 1px solid black;}
input.valid { border: 1px solid green;}
input.invalid { border: 1px solid red;}
input.invalid + .error-message { display: initial;}
.error-message { display: none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type="text" id="some-number">

How to retrieve textfield value in real time?

You can try with onKeyUp event in javascript. onkeyup

Swift checking textfield input live

Listen for UIControlEventEditingChanged events from the text field. Register for them either with

  1. the Interface Builder: drag from the text field to the file, and select the action type "Editing Changed"

  2. the following code:

    override func viewDidLoad() {
    super.viewDidLoad()
    // ...
    textField.addTarget(self, action:"edited", forControlEvents:UIControlEvents.EditingChanged)
    }

    func edited() {
    println("Edited \(textField.text)")
    }

How to validate text box inputs in real-time

For your first question, there are two ways to do it. Either you use the trace method on your StringVar, or use validcommand on your entry. You can read up the details on how to use both methods here and here

import tkinter as tk

root = tk.Tk()

# Use trace method on your StringVar

text_Input = tk.StringVar() # note that it is StringVar() with ()
txtbox = tk.Entry(font="Arial 20 bold",textvariable=text_Input)
txtbox.grid(columnspan = 2, row = 3, pady = 20)

def trace_method(*args):
if text_Input.get().isdigit():
pass
else:
text_Input.set(text_Input.get()[:-1])

text_Input.trace("w",trace_method)

# Use validatecommand attribute of entry widget

def onValidate(S):
if S.isdigit():
return True
else:
return False

vcmd = (root.register(onValidate),"%S")

txtbox2 = tk.Entry(font="Arial 20 bold",validate="key",validatecommand=vcmd)
txtbox2.grid(columnspan = 2, row = 4, pady = 20)

root.mainloop()

For your second question, I can't fully understand what you are trying to achieve, but if the problem lies with the binding with key f, i suppose you can simply call game.unbind('f') in your submit function.

How to detect live changes on TextField in SwiftUI?

You can create a binding with a custom closure, like this:

struct ContentView: View {
@State var location: String = ""

var body: some View {
let binding = Binding<String>(get: {
self.location
}, set: {
self.location = $0
// do whatever you want here
})

return VStack {
Text("Current location: \(location)")
TextField("Search Location", text: binding)
}

}
}


Related Topics



Leave a reply



Submit