I’d like to know how to do the following with Jquery:
I have 1 textfield in a form.
Whenever the first character is a number, change the attr name of this field to ‘number’. If the first character is a letter, change this attr name to ‘letter’.
!! This also has to work when a number or text is copy-pasted into the field.
Thanks!
Jeroen
Bind (some) event(s) to the text field: The
keyupevent is used to update thenameattribute when the user modifies the text (including shortcutCTRL+Vcopy-pasting),the
pasteandmousemoveevents are used to deal with copy-pasting (dragging, contextmenu).Remove
else {...}, and replaceelse if(/[a-zA-Z]/.test(char)) {withelse {if you want the default name to beletter.