Would someone be able to help me with some javascript code please that will alert on submission of a form if there's a ' character (apostrophe) within a textbox?
Thank you.
Would someone be able to help me with some javascript code please that will alert on submission of a form if there's a ' character (apostrophe) within a textbox?
Thank you.
You can use String.prototype.indexOf
to check for apostrophe's presence:
var str = "That's awesome";
if(str.indexOf("'") !== -1) {
alert("no apostrophe's allowed");
}
Doing it on form submission is a separate issue.
Add keyUp event listener to the textbox. Check the keyCode of the key pressed. If it matched for keyCode of '
, alert the user. After that, replace the '
with a blank.
document.getElementById('input').addEventListener('keyup', function(e){
if(e.keyCode === 222){ // check if the key pressed is '
alert('No Single Quotes Please!');
this.value = this.value.replace("'", ""); // replace ' with a blank
}
}, false);
<input type="text" id="input" />
Readup on these: