Preventing user to have same value as the placeholder
← Examples
This example shows a few ways to prevent user to put the same value as the placeholder attribute.
For instance, we could show an error message when someone enters First name or Last name for the text fields named firstName
and lastName
, respectively.
Using callback validator
The first approach is to use the callback validator:
As you saw, the field passes the callback
validator if its value (you can use the value
passed as a parameter of callback, or $field.val()
) is different than its placeholder attribute which can be accessed as $field.attr('placeholder')
.
Creating a custom validator
It's also possible for you to develop a custom validator for that purpose and reuse it whenever you want.
The following code snippet creates a validator named placeholder
(of course, you can change it):
Now, you can apply the created validator with multiple fields:
The following form uses both approaches which are described above: