Jump to content


Photo

java script to validate and accept only specific words

java

  • Please log in to reply
4 replies to this topic

#1 cainam29

cainam29

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts

Posted 09 September 2013 - 01:11 PM

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


#2 Irate

Irate

    Advanced Member

  • Members
  • PipPipPip
  • 358 posts
  • LocationHamburg, Germany
  • Age:17

Posted 09 September 2013 - 01:18 PM

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.
Quod placet mihi non placeat tibi. - What I think to be good must not always equal your perception of it.

I am not perfect. I try a lot with the code I provide and I don't guarantee for it to work as I have mostly no option to test it on my mobile phone. I do apologize for any inconvenience I caused, but if I do happen to have helped, liking my posts or marking them as to have solved or answered your question would be nice.

#3 DavidAM

DavidAM

    Advanced Member

  • Gurus
  • 1,976 posts
  • LocationSpring, TX USA

Posted 09 September 2013 - 02:00 PM

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.
-- I haven't lost my mind, it's backed up on tape ... somewhere!

#4 priyankagound

priyankagound

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 18 September 2013 - 05:13 AM

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>


#5 Irate

Irate

    Advanced Member

  • Members
  • PipPipPip
  • 358 posts
  • LocationHamburg, Germany
  • Age:17

Posted 18 September 2013 - 08:07 AM

Just that validate() returns undefined if a match was not found. Have it return true to continue form submission.
Quod placet mihi non placeat tibi. - What I think to be good must not always equal your perception of it.

I am not perfect. I try a lot with the code I provide and I don't guarantee for it to work as I have mostly no option to test it on my mobile phone. I do apologize for any inconvenience I caused, but if I do happen to have helped, liking my posts or marking them as to have solved or answered your question would be nice.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com