cainam29 Posted September 9, 2013 Share Posted September 9, 2013 need help java script below not working, its supposed to allow only the word "Resolved" and "Re-assigned" in one of my text area, so if the user enter other values, the form will not be submitted... function checkAllowedWords(){ var allowedWords, textString, textArray, length, word; allowedWords=["Resolved","Re-assigned"]; textString= document.getElementById("$Status").value; //replace the "text-area-element-id" with your actual id for that textarea textArray=str.split(" "); //if comma separated values are provided use "," instead of " " length = textArray.length; word = null; for (var i = 0; i < length; i++) { word = textArray[i]; if (allowedWords.indexOf(word, 0) === -1) { //indexOf() returns the index of the first "word" value found in the "allowedWords" array, and returns -1 if the value was not found in array alert(word + " is not allowed! The accepted values are: " + allowedWords.toString()); //This will popup an alert message for your testing purposes, you can implement anything you wish here to let the user know what's going on } } } Quote Link to comment https://forums.phpfreaks.com/topic/282019-java-script-to-validate-and-accept-only-specific-words/ Share on other sites More sharing options...
Irate Posted September 9, 2013 Share Posted September 9, 2013 I wouldn't ever validate any input with JavaScript... Any user with adequate knowledge can simply modify your script to allow quite a number of things, mostly things you don't want to appear. Solution. Parse the data server-side after being submitted, escape it properly and check for your words, if you found them, continue displaying them. Otherwise, echo an error message. Quote Link to comment https://forums.phpfreaks.com/topic/282019-java-script-to-validate-and-accept-only-specific-words/#findComment-1448885 Share on other sites More sharing options...
DavidAM Posted September 9, 2013 Share Posted September 9, 2013 Why use TEXT AREA field if the input is restricted to one word out of a specific list. Event if you have 10 or so allowed words, you should probably be using a SELECT element. Then the JavaScript is not really even needed. It is true that you can NOT rely on JavaScript validation to prevent faulty input. However, it can improve the user's experience to verify the data in JavaScript. The user gets immediate feedback on potential problems, and you save your server the extra load of processing bad data. However, you MUST validate at the server since the user CAN disable JAVASCRIPT, or re-write the form entirely. Quote Link to comment https://forums.phpfreaks.com/topic/282019-java-script-to-validate-and-accept-only-specific-words/#findComment-1448893 Share on other sites More sharing options...
priyankagound Posted September 18, 2013 Share Posted September 18, 2013 try out with the below example: <script type="text/javascript"> function validate() { var regexp1=new RegExp("(word1|word2)"); if(regexp1.test(document.getElementById("txt").value)) { alert("Word found in string"); return false; } } </script> <form action="" method="post" onsubmit="return validate()"> <input type="text" id="txt" name="txt"> <input type="submit" value="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/282019-java-script-to-validate-and-accept-only-specific-words/#findComment-1450035 Share on other sites More sharing options...
Irate Posted September 18, 2013 Share Posted September 18, 2013 Just that validate() returns undefined if a match was not found. Have it return true to continue form submission. Quote Link to comment https://forums.phpfreaks.com/topic/282019-java-script-to-validate-and-accept-only-specific-words/#findComment-1450051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.