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:
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:
if(str.indexOf('%') != str.length -1) {
// alert something
}
2020 edit, use string.endsWith()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With