lundi 2 mars 2015

If statement condition based on regular expression being met when it shouldn't

I have developed some Javascript which looks out for certain characters being put into text fields and removes them, then displays a pop up message to the user. For some reason, the pop up message is showing when you click in and out of any text field, suggesting to me that the if condition inside the punctuationReplace function below is always being met. I'm not sure why this is - does anyone have any ideas?



var inputs = document.getElementsByTagName("input");
var exceptions = ['IPQ_G_QH_IST_NA1', 'IPQ_G_QH_IST_NA7', 'IPQ_G_QH_IST_NA3', 'IPQ_G_QH_IST_NA4', 'IPQ_G_QP_IST_NM1', 'IPQ_G_QP_IST_NM2'];
var i = 0;
for (i = 0; i < inputs.length; i++) {
if (inputs[i].type === "text" && exceptions.indexOf(inputs[i].id) == -1) {
inputs[i].addEventListener('blur', punctuationReplace, false);
}
}

function punctuationReplace() {
if (/("|'|<|>)/g.test(this.value) === true) {
this.value = this.value.replace(/("|'|<|>)/g, '').replace(/&/g, 'and');
var buttons = {};
buttons["OK"] = function() {
sits_dialog_close(true);
};
sits_dialog("Warning", "Sorry, you have used a prohibited character. The following characters cannot be used in this application for security reasons: double quotes (\"), apostrophe (\'), ampersand (&), greater than symbol (\>) and less than symbol (\<). We apologise for any inconvenience caused.", buttons, true, true, false);
return;
}
}

Aucun commentaire:

Enregistrer un commentaire