SnakesBite101 Posted June 8, 2012 Share Posted June 8, 2012 i understand this explained but can't even get it to work. please copy and paste my code here: http://www.w3schools.com/js/tryit.asp?filename=tryjs_form_validation and please explain to me why my alert msg isnt displaying. what am i doing wrong? thanks <!DOCTYPE html><html><head><script type="text/javascript"> function validate()var x = document.forms.test.userName.value;if(x.length==0){alert("please enter a username");return false;} </script></head><body> <form id="test" method="post" onsubmit="return validate()">Username: <input type="text" id="userName"/> <br/>Password: <input type="password" id="password"/> <br/><input type="submit" value="sign in"/></form> </body></html> Link to comment Share on other sites More sharing options...
Ingolme Posted June 8, 2012 Share Posted June 8, 2012 You're missing an opening curly bracket { after this line: function validate() Try accessing the element in a standard way: var x = document.getElementById("userName").value; The way you have it most likely only works in very old versions of Internet Explorer. 1 Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 8, 2012 Author Share Posted June 8, 2012 (edited) thanks very much it worked! can you or anyone else please help me out on this one aswell copy and paste code here: http://www.w3schools...form_validation <!DOCTYPE html><html><head><script type="text/javascript">function month(){var x = new date().getDate()if (x <= 11) { alert("not yet time, please be patient");}else if (x >= 11){ alert("sorry you missed the deadline");}}</script></head><body><form><input type="submit" value="Submit" onclick="month()"></form></body></html> Edited June 8, 2012 by SnakesBite101 Link to comment Share on other sites More sharing options...
Ingolme Posted June 8, 2012 Share Posted June 8, 2012 You don't need a form or an input button for this. Just use <input type="button"> and remove the <form> tags. The form requires an action and a method attribute. As for the javascript, Date() requires an uppercase D, and new Date() should be within parenthesis: var x = (new Date()).getMonth(); 1 Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 8, 2012 Author Share Posted June 8, 2012 You don't need a form or an input button for this. Just use <input type="button"> and remove the <form> tags. The form requires an action and a method attribute. As for the javascript, Date() requires an uppercase D, and new Date() should be within parenthesis: var x = (new Date()).getMonth(); thanks i really appreciate the help ingolme. i dont think i ever saw new Date() being inside brackets/parenthesis. i'll definately include this from now on. Link to comment Share on other sites More sharing options...
eTianbun Posted June 8, 2012 Share Posted June 8, 2012 thanks i really appreciate the help ingolme. i dont think i ever saw new Date() being inside brackets/parenthesis. i'll definately include this from now on. Not all the time, but in some cases. Link to comment Share on other sites More sharing options...
Ingolme Posted June 8, 2012 Share Posted June 8, 2012 If you don't put the parenthesis, the program supposes that (date().getMonth()) returns a function. Link to comment Share on other sites More sharing options...
eTianbun Posted June 8, 2012 Share Posted June 8, 2012 (edited) If you don't put the parenthesis, the program supposes that (date().getMonth()) returns a function. exactly, so dont GO start doing something like this:x=(new Object())y=x.property() It's not necessary! You only enclose it with parenthesis, when you want to seperate the the constructed object from its property, in a single line: (new Object()).property() Edited June 8, 2012 by CodeName Link to comment Share on other sites More sharing options...
Ingolme Posted June 8, 2012 Share Posted June 8, 2012 exactly, so dont GO start doing something like this:x=(new Object())y=x.property() It's not necessary! You only enclose it with parenthesis, when you want to seperate the the constructed object from its property, in a single line: (new Object()).property() That's what I was referring to. I'm not saying it needs parenthesis unless you're trying to access a property or method. Link to comment Share on other sites More sharing options...
eTianbun Posted June 8, 2012 Share Posted June 8, 2012 That's what I was referring to. I'm not saying it needs parenthesis unless you're trying to access a property or method. Yes bro, i know, i was refering to the OP, about the last statement (s)he made:...I dont think i ever saw new Date() being inside brackets/parenthesis. i'll definately include this from now on. Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 8, 2012 Author Share Posted June 8, 2012 (edited) thanks guys i undersrtand. i'm getting it a lot more now. I've been playing around with user data, and i was trying something but have'nt got it yet. here's what i'm trying... i prompt the user to enter a last name which i will store in a lname variable. once i've recieved the value, i change the variable so that i automatically capatilise the 1st letter. Then send an alert to the user displaying his last name with the 1st letter capatilised. here's the code im trying with no luck yet. var lnamelname = prompt("what is your last name?","");lname = lname.substr(0,1).toUpperCase();alert("Your last name is " + lname); substr, substring etc capitalise the 1st letter but deletes the rest of the string. if i use substring(0,5) it capitalises every word. please any help on how i can just cap the 1st letter with the rest remaining lower case? Edited June 8, 2012 by SnakesBite101 Link to comment Share on other sites More sharing options...
eTianbun Posted June 8, 2012 Share Posted June 8, 2012 EX: FL=lname.substr(0,1).toUpperCase()RL=lname.substr(1,lname.length)AL=FL+RLalert(AL) 1 Link to comment Share on other sites More sharing options...
thescientist Posted June 8, 2012 Share Posted June 8, 2012 (edited) read the documentation. You will see that those functions have a return value, and that is what you are assigning to lname. Consider something like this: var lname = ''; lname = prompt("what is your last name?","");lname = lname[0].toUpperCase() + lname.substr(1); alert("Your last name is " + lname); Edited June 8, 2012 by thescientist Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 8, 2012 Author Share Posted June 8, 2012 thanks a bunch codename it worked perfectly:) Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 8, 2012 Author Share Posted June 8, 2012 read the documentation. You will see that those functions have a return value, and that is what you are assigning to lname. Consider something like this:var lname = ''; lname = prompt("what is your last name?","");lname = lname[0].toUpperCase() + lname.substr(1); alert("Your last name is " + lname); hmm for some reason this code is failing to alert me, not sure why Link to comment Share on other sites More sharing options...
thescientist Posted June 8, 2012 Share Posted June 8, 2012 (edited) hmm for some reason this code is failing to alert me, not sure whythere must be something else in your code then (or there are errors you aren't checking for), because that example works.http://jsfiddle.net/ujpPB/ Edited June 8, 2012 by thescientist Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 8, 2012 Author Share Posted June 8, 2012 there must be something else in your code then (or there are errors you aren't checking for), because that example works.http://jsfiddle.net/ujpPB/ it must be something wrong on my end but on that site you gave me, without all my code, it prompts me for a last name, but i don't get an alert of my last name. Link to comment Share on other sites More sharing options...
thescientist Posted June 8, 2012 Share Posted June 8, 2012 could be your browser then. Not sure what to say. Link to comment Share on other sites More sharing options...
eTianbun Posted June 8, 2012 Share Posted June 8, 2012 it must be something wrong on my end but on that site you gave me, without all my code, it prompts me for a last name, but i don't get an alert of my last name.That should work perfectly! Link to comment Share on other sites More sharing options...
Ingolme Posted June 8, 2012 Share Posted June 8, 2012 I don't think Javascript supports array notation for strings. Instead of lname[0] try lname.charAt(0) Link to comment Share on other sites More sharing options...
thescientist Posted June 8, 2012 Share Posted June 8, 2012 good point Ingolme. Maybe it depends on the browser then. Anyway, here's the updated code. var lname = ''; lname = prompt("what is your last name?","");lname = lname.charAt(0).toUpperCase() + lname.substr(1); alert("Your last name is " + lname); http://jsfiddle.net/f95VL/ Link to comment Share on other sites More sharing options...
eTianbun Posted June 8, 2012 Share Posted June 8, 2012 (edited) I guess Its the browser... it works for me tho! Edited June 8, 2012 by CodeName Link to comment Share on other sites More sharing options...
SnakesBite101 Posted June 8, 2012 Author Share Posted June 8, 2012 good point Ingolme. Maybe it depends on the browser then. Anyway, here's the updated code. var lname = ''; lname = prompt("what is your last name?","");lname = lname.charAt(0).toUpperCase() + lname.substr(1); alert("Your last name is " + lname); http://jsfiddle.net/f95VL/ hey thanks the new code works fine. i knew it was on my end. i recently reformatted my PC and i havent yet installed the new IE yet, still on IE 7:) i guess thats a good thing as i've learnt more today. thanks again Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now