kanala Posted June 15, 2007 Share Posted June 15, 2007 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 More sharing options...
zppblood Posted June 15, 2007 Share Posted June 15, 2007 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. Link to comment Share on other sites More sharing options...
kanala Posted June 15, 2007 Author Share Posted June 15, 2007 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 More sharing options...
zppblood Posted June 15, 2007 Share Posted June 15, 2007 http://w3schools.com/js/default.asp Link to comment Share on other sites More sharing options...
kanala Posted June 15, 2007 Author Share Posted June 15, 2007 http://w3schools.com/js/default.aspOK 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 More sharing options...
kanala Posted June 15, 2007 Author Share Posted June 15, 2007 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 More sharing options...
zppblood Posted June 15, 2007 Share Posted June 15, 2007 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 More sharing options...
kanala Posted June 15, 2007 Author Share Posted June 15, 2007 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 More sharing options...
Jamess Posted June 16, 2007 Share Posted June 16, 2007 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 More sharing options...
kanala Posted June 16, 2007 Author Share Posted June 16, 2007 <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 More sharing options...
justsomeguy Posted June 18, 2007 Share Posted June 18, 2007 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 More sharing options...
richardgerbino Posted June 19, 2007 Share Posted June 19, 2007 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 More sharing options...
kanala Posted June 23, 2007 Author Share Posted June 23, 2007 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 More sharing options...
justsomeguy Posted June 25, 2007 Share Posted June 25, 2007 No problem. Link to comment Share on other sites More sharing options...
louieaw Posted August 13, 2007 Share Posted August 13, 2007 Hey, where would I put the link to redirect in the script? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 13, 2007 Share Posted August 13, 2007 The redirect_url variable will contain the link to redirect to, it gets created in the loop. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.