Jump to content

Archived

This topic is now archived and is closed to further replies.

cainam29

java script to validate and accept only specific words

Recommended Posts

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
  }
}
}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites

Just that validate() returns undefined if a match was not found. Have it return true to continue form submission.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.