-1

I want to show an error/alert message if the user's input a particular values(% or *). If the user enter just % or * and hit 'submit' then an alert or message should say "This is not allowed". However, SM%TH or *MITH should be allowed.

HTML

<input type="text" name="ANSWER.TTQ.MENSYS.3." id="ANSWER.TTQ.MENSYS.3." value="" class="forminfree">
<input type="submit" name="ANSWER.TTQ.MENSYS.7." id="ANSWER.TTQ.MENSYS.7." value="Search" class="formsubfree" style="width:100px" onclick="searchCheck()">

SCRIPT

function searchCheck(){
}

As always your help will be highly appreciated.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
19eggs
  • 205
  • 3
  • 15

4 Answers4

2

Try this:

function searchCheck(){
     var answer = $.trim($(this).find('input[type=text]').val());
     if(answer == '*' || answer == '%') alert('this is not allowed');
}
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
0

First fetch the value. With jQuery:

var str = $("#ANSWER.TTQ.MENSYS.3.").val();

With KY Without jQuery:

var elem = document.getElementById("ANSWER.TTQ.MENSYS.3.");
var str = elem.value;

Now you just check it out:

str = str.trim();
if (str == "*" || str == "%") {
    alert("This is not allowed. Get off my system!");
}
Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
  • 1
    Hi! Renan, I have not tried your code, but I am sure it would work as it is similar to VeeKayBee's reply. Thank you. – 19eggs Jul 23 '13 at 15:32
0

There might be having issues while accessing the ID which is having a dot(.). Inorder to avoid that you must use $('#ANSWER\\.TTQ\\.MENSYS\\.3\\.') this way.

  function searchCheck(){
         var answer = $.trim($('#ANSWER\\.TTQ\\.MENSYS\\.3\\.').val());
         if(answer == '*' || answer == '%') alert('this is not allowed');
    }

Demo here : http://jsfiddle.net/Kk8GC/

kbvishnu
  • 14,760
  • 19
  • 71
  • 101
  • Hi! VeeKayBee..I had this yesterday without the $('#ANSWER\\.TTQ\\.MENSYS\\.3\\.'). Thank you. Also, after clicking on the alert the form gets submitted. return false does not stop it. – 19eggs Jul 23 '13 at 15:26
  • @19eggs Great !!!. Happy that you got a solution. I am sorry that I saw your comment now only and happy that you fixed by yourself. :) – kbvishnu Jul 23 '13 at 17:47
  • @19eggs refer this http://stackoverflow.com/questions/605630/how-to-select-html-nodes-by-id-with-jquery-when-the-id-contains-a-dot But I think some browsers it may not work. So better to use underscores(_) instead of dots(.) – kbvishnu Jul 23 '13 at 17:53
0

Crawl over the string, check for preceeding * or % and stop with follow up of ** or %* but not %%.

function searchCheck(){

var pos=0;
var error=false;
var prior=false;

while(pos<your_string.length && !error){
  if(prior){
    if(your_string.charAt(pos) == '*'){
      error = true;
    } else if(your_string.charAt(pos) != '%') prior = false;
  } else { 
    if(your_string.charAt(pos) == '%' || your_string.charAt(pos) == '*')
      prior = true;   
  }
  pos++;
}
if(error || (your_string.length==1 && prior)){
    alert("not allowed");
}

}
Lemonade
  • 503
  • 2
  • 8
  • Hi! Lemmy, I have decided to use VeeKayBee's answer as it is shorter. But thank you for taking your time and replying. – 19eggs Jul 23 '13 at 15:38