Jump to content

very simple form validation via condition help needed


SnakesBite101
 Share

Recommended Posts

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

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.

  • Like 1
Link to comment
Share on other sites

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 by SnakesBite101
Link to comment
Share on other sites

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();

  • Like 1
Link to comment
Share on other sites

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

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 by CodeName
Link to comment
Share on other sites

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

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

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 by SnakesBite101
Link to comment
Share on other sites

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 by thescientist
Link to comment
Share on other sites

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

hmm for some reason this code is failing to alert me, not sure why
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/ Edited by thescientist
Link to comment
Share on other sites

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

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

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

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...