April 25, 201214 yr I want user to enter there name in text field, but it cant contain numbers only letters. how can i do this??? here is my code function validator() { var jerseynumber = document.getElementById("jersey_number").value; var firstname = document.getElementById("firstname").value; if (isNaN(jerseynumber)||jerseynumber<1||jerseynumber>100) { alert("Not a valid number"); } if (firstname!=[A-Z,a-z]) { alert("Not a valid number"); } } Edited April 25, 201214 yr by Renaissance-Design Please use the code button or tags to format your code.
April 26, 201214 yr Hi, Make sure you are checking every letter in the firstname. I don't know how you want your program to react when it finds a number but one way is to use a loop to go through each character in the string and check to see that it is not a number. There are other faster ways like using regular expressions but that one is easy to process in the head. My head. Edited April 26, 201214 yr by rrr
April 26, 201214 yr If you're just checking for the presence of a number: if (/\d/.test(firstname)) { alert("Ooh, you put a number in your firtname. How do you pronounce that you mofo?!"); }
April 26, 201214 yr It's out! The regex is OUT!!!! HE BROUGHT THE REGEX OUT! Edited April 26, 201214 yr by rrr
April 26, 201214 yr It's ok. I'll just pretend it's a typo so it doesn't make me wonder why a bunch of characters do the same work as ten lines.
Create an account or sign in to comment