I am getting value from a text field. I want to show an alert message if a special character, say % doesn’t appear at the end of entered input.
Usecases:
- ab%C – show alert
- %abc- show alert
- a%bc- show alert
- abc%- ok
The regex i came up so far is this.
var txtVal = document.getElementById("sometextField").value;
if (!/^[%]/.test(txtVal))
alert("% only allowed at the end.");
Please help.
Thanks
No need for a regex. indexOf will find the first occurrence of a character, so just check it it’s at the end:
2020 edit, use string.endsWith()