Jump to content

java script to validate and accept only specific words


cainam29

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
  }
}
}
Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

  • 2 weeks later...
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.