Jump to content

Input text box allow specific text


kanala
 Share

Recommended Posts

How would I make an input text box that only allows certain things, and if you submit something else, an alert pops up saying "incorrect name" or something.Example names:JohnnyLukeJackJohnChrisetc

Link to comment
Share on other sites

Create an array with JavaScript and check to see if the text matches one of the names in the array. Use onsubmit in the form element if the button type is "submit", or use onclick in the input element of the "button" type.
Oh yea I forgot to tell you, I know nothing about javascript. :)
Link to comment
Share on other sites

OK i'v created an array, how do I make it find out if the text inputted is the same as one of the arrays?<script type="text/javascript">var names = new Array();names[0] = "01";names[1] = "02";names[2] = "03";names[3] = "04";names[4] = "05";names[5] = "06";names[6] = "07";</script>The input box<input type="text" id="name" " size="20"><button onclick="...">Submit</button>
Link to comment
Share on other sites

OK i'v created an array, how do I make it find out if the text inputted is the same as one of the arrays?<script type="text/javascript">var names = new Array();names[0] = "01";names[1] = "02";names[2] = "03";names[3] = "04";names[4] = "05";names[5] = "06";names[6] = "07";</script>The input box<input type="text" id="name" " size="20"><button onclick="...">Submit</button>
Using my limited skills I made up this:<script type="text/javascript">var xvar names = new Array();names[0] = "01";names[1] = "02";names[2] = "03";names[3] = "04";names[4] = "05";names[5] = "06";names[6] = "07";function submit(name){for (x in names) {if (name.value == names[x]) {{window.location.replace("TD" + name + ".html")}else{alert("Incorrect Order Number")}}}}</script> <input type="text" id="name" size="20" /><button onclick="submit(name)">Submit</button>NOT RIGHT? :)
Link to comment
Share on other sites

Try this

<script type="text/javascript">  var x  var names = new Array();  names[0] = "01";  names[1] = "02";  names[2] = "03";  names[3] = "04";  names[4] = "05";  names[5] = "06";  names[6] = "07";  function submit(name) {    for (x in names) {      if (name == names[x]) {        window.location="TD" + name + ".html"      }      else {        alert("Incorrect Order Number")      }    }  }</script><form>  <input type="text" id="name" size="20" />  <input type="button" onclick="submit(document.getElementById('name').value)" value="Submit"></form>

Link to comment
Share on other sites

Try this
<script type="text/javascript">  var x  var names = new Array();  names[0] = "01";  names[1] = "02";  names[2] = "03";  names[3] = "04";  names[4] = "05";  names[5] = "06";  names[6] = "07";  function submit(name) {    for (x in names) {      if (name == names[x]) {        window.location="TD" + name + ".html"      }      else {        alert("Incorrect Order Number")      }    }  }</script><form>  <input type="text" id="name" size="20" />  <input type="button" onclick="submit(document.getElementById('name').value)" value="Submit"></form>

Doesn't work, when I click submit the page reloads.
Link to comment
Share on other sites

How would I make an input text box that only allows certain things, and if you submit something else, an alert pops up saying "incorrect name" or something.Example names:JohnnyLukeJackJohnChrisetc
<input type="text" name="input" /><? $check = $_POST['input'];if ($check != "Johnny") {print "Your input does not say Johnny";}elseif ($check != "Luke") {print "Your input does not say Luke";}elseif ($check != "Jack") {print "Your input does not say Jack";}else {print "Your input is either Johnny, Luke or Jack";}

Link to comment
Share on other sites

<input type="text" name="input" /><? $check = $_POST['input'];if ($check != "Johnny") {print "Your input does not say Johnny";}elseif ($check != "Luke") {print "Your input does not say Luke";}elseif ($check != "Jack") {print "Your input does not say Jack";}else {print "Your input is either Johnny, Luke or Jack";}

What if I needed to add more names? Is there a way of doing it by using Array?
Link to comment
Share on other sites

Using your original code..

<script type="text/javascript">  var names = new Array();  names[0] = "01";  names[1] = "02";  names[2] = "03";  names[3] = "04";  names[4] = "05";  names[5] = "06";  names[6] = "07";  function test_name(name) {	var name_found = false;	for (x in names) {	  if (name == names[x]) {		name_found = true;		redirect_url = "TD" + name + ".html"; //if you want them to redirect	  }	}	if (name_found)	{	  window.location = redirect_url;	}	else	{	  alert("Name not found");	}  }</script><form>  <input type="text" id="name" size="20" />  <input type="button" onclick="test_name(document.getElementById('name').value)" value="Submit"></form>

I also renamed the function from "submit" to "test_name", the form object already has a method called submit.

Link to comment
Share on other sites

The code Jamess supplied is the best way to go if you have a PHP enabled server. Just save your page with a .php extension (unless your .htaccess is configured to parse every page for php. if you dont know what that means, just save it with a .php at the end) and his code will work.If you want to add more names just add the following code as many times over as you want:

}elseif ($check != "NewName1") {print "Your input does not say NewName1";}

You can edit anything in quotations without endangering the code.Remember, if your file doesn't have a .php at the end, this won't work.

Link to comment
Share on other sites

Using your original code..
<script type="text/javascript">  var names = new Array();  names[0] = "01";  names[1] = "02";  names[2] = "03";  names[3] = "04";  names[4] = "05";  names[5] = "06";  names[6] = "07";  function test_name(name) {	var name_found = false;	for (x in names) {	  if (name == names[x]) {		name_found = true;		redirect_url = "TD" + name + ".html"; //if you want them to redirect	  }	}	if (name_found)	{	  window.location = redirect_url;	}	else	{	  alert("Name not found");	}  }</script><form>  <input type="text" id="name" size="20" />  <input type="button" onclick="test_name(document.getElementById('name').value)" value="Submit"></form>

I also renamed the function from "submit" to "test_name", the form object already has a method called submit.

Wow Thanks it works!!!
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...