0

Is it possible using Javascript (or any other language if not) to detect if a user types in the word URGENT (any case) or *URGENT* as the first word in a text field? If so, could I get the form to pop up a message either on submit or when they type it asking them politely not to start their call with this? If they include the word anywhere else then this is fine, it's just at the start that we don't want it. Thank you.

Maff
  • 421
  • 1
  • 5
  • 19
  • 3
    Yes, it's possible, but you need to show us that you've tried something already. We can't do the work for you. – Andy Feb 18 '14 at 15:51
  • 1
    This is actually several questions about how to perform some basic tasks in Javascript. I think we shouldn't be so fast to demand of new users, "What have you tried?" Is it necessary to take a shot in the dark before asking for assistance? – Vivian River Feb 18 '14 at 16:02
  • I should have mentioned, I don't know JavaScript, I know PHP but wouldn't know where to begin with JS. I could do it in PHP but not until the form's been submitted which wouldn't be good enough in this case. – Maff Feb 18 '14 at 16:04
  • Time to get to know JavaScript, then. http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742/ref=sr_1_1?ie=UTF8&qid=1392740397&sr=8-1&keywords=javascript+good+parts – Vivian River Feb 18 '14 at 16:20
  • I also wrote my own introduction to Javascript a while back. It's incomplete and rough around the edges. http://danielsadventure.info/Javascript/Javascript.html – Vivian River Feb 18 '14 at 16:21
  • This is the only thing I need in JS though, I cannot find the solution anywhere, maybe I'm searching using the wrong wording? – Maff Feb 18 '14 at 16:22
  • I think that we've provided plenty of information here for you to get started. Why don't you see what you can do with this information, and if you still need help, ask a more specific question about what you are stuck on? – Vivian River Feb 18 '14 at 16:32

2 Answers2

1

With some little research you can know that the answer to your question is yes. Your question has been asked and answered many times, therefore it will better for your learning expericence to do some research about it.

How? Kindly read:

  1. developer.mozilla String/indexOf
  2. Javascript indexOf case insensitive

    //first step, catch the text that user has entered from an input with the id someTextField
    var str1 =document.getElementById("someTextField").value;
    //assign some values to search for, words listed here will trigger the alert. 
    var wordsToFind = ["urgent", "*urgent*"];
    //for now, we will take the user's input, str1, and convert it to lower case, then try to check if the first word (indexOf)the words to find is 0. 0 means at the beginning of the text. 
      //find first word in array
     if (str1.toLowerCase().indexOf(wordsToFind[0]) === 0 
           //find second word in array by adding OR || 
         || str1.toLowerCase().indexOf(wordsToFind[1]) === 0) {      
        //display a message to inform the user.   
        alert("Kindly you can not start a message with the word 'urgent'");            
    }
    

If you have many words, then you can put them all in the array and loop through it and try to see if the user's text contain any of the blacklisted words.

Community
  • 1
  • 1
Mohammed Joraid
  • 4,974
  • 2
  • 20
  • 35
1

First, you will need to get the text from the textbox with something like

var s = document.getElementById('myTextBox').value;

Once you have that, there are a number of ways you could check to see if it starts with the word "URGENT". One example would be

var containsUrgent = s.substr(0, 6).toUpperCase() === 'URGENT';

Once you've done that, the simplest way to display the message on the screen would be

alert('The string in the textbox starts with URGENT');

This is not a best practice, but it should help you get started.

If you want to alert the message when the user types or submits the form, you can attach your code to the submit and keypress events.

Vivian River
  • 28,530
  • 54
  • 179
  • 298