/    Sign up×
Community /Pin to ProfileBookmark

Submit Link Trouble

Hello everyone,

I know zilch about javascript. If its not a cut-n-past script I’m pretty much SOL.

So, this may seem like a very simple question, but I cant’ figure out how to validate my form fields through my submit link.

Here is my validate code:

[code=php]
<script language=”JavaScript” type=”text/javascript”>
<!–
function checkForm(theForm)
{
if (theForm.firstname.value == “”)
{
alert(“Please enter a value for the “firstname” field.”);
theForm.firstname.focus();
return (false);
}
if (theForm.lastname.value == “”)
{
alert(“Please enter a value for the “lastname” field.”);
theForm.lastname.focus();
return (false);
}
if (theForm.username.value == “”)
{
alert(“Please enter a value for the “username” field.”);
theForm.username.focus();
return (false);
}
if (theForm.email.value == “”)
{
alert(“Please enter a value for the “email” field.”);
theForm.email.focus();
return (false);
}
if (theForm.password.value == “”)
{
alert(“Please enter a value for the “password” field.”);
theForm.password.focus();
return (false);
}
if (theForm.password.value != theForm.confirm.value)
{
alert(“The two passwords are not the same.”);
theForm.confirm.focus();
return (false);
}
}

//–>
</script>
[/code]

and my form / submit link looks like this right now:

[code=php]
<form method=”post” action=”register.php” onsubmit=”return checkForm(this)” language=”JavaScript” name=”register”>

<a href=”javascript:document.register.submit()” class=”nav”>Register</a>
</form>
[/code]

Hopefully someone will understand what I’m trying to say and accomplish. Don’t laugh at me too hard if this is the dumbest question to be asked thus far ?

Thanks for your help. It is greatly appreciated!

Cheers,

Dave

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@tabzterOct 10.2005 — change this:
[code=php]
<form method="post" action="register.php" onsubmit="return checkForm(this)" language="JavaScript" name="register">
<a href="javascript:document.register.submit()" class="nav">Register</a>
</form>[/code]


to this:
[code=php]
<form method="post" action="register.php" onsubmit="return checkForm(this)" name="register">
First Name: <input type="text" name="firstname" /><br />
Last Name: <input type="text" name="lastname" /><br />
UserName: <input type="text" name="username" /><br />
Email: <input type="text" name="email" /><br />
Password: <input type="password" name="password" /><br />
Confirm Password: <input type="password" name="confirm" /><br />
<input type="submit" value="Register" />
</form>[/code]
Copy linkTweet thisAlerts:
@dave37authorOct 10.2005 — Tabzter,

Thank you for your input.

I already have all of what you posted, i just didn't find it necessary to post the individual text fields.

What I'm really looking for is how to validate and submit the form through a text link. Not via a submit button.

Any ideas?

Cheers,

Dave
Copy linkTweet thisAlerts:
@UltimaterOct 10.2005 — Give this a try:
<i>
</i>&lt;a href="javascript:[color=royalblue]void([/color]document.register.submit()[color=royalblue])[/color]" class="nav"&gt;Register&lt;/a&gt;
Copy linkTweet thisAlerts:
@MjhLkwdOct 10.2005 — Dave:

"password" and "confirm" are reserved words, and shouldn't be used as field names.

[CODE]<HTML>
<Head>
<Script type="text/javascript">

function checkForm(theForm){

var isValid = true;

if (theForm.firstname.value == "")
{
alert("Please enter a value for the First Name field.");
theForm.firstname.focus();
isValid = false;
}

if (isValid && theForm.lastname.value == "")
{
alert("Please enter a value for the Last Name field.");
theForm.lastname.focus();
isValid = false;
}

if (isValid && theForm.username.value == "")
{
alert("Please enter a value for the User field.");
theForm.username.focus();
isValid = false;
}

if (isValid && theForm.email.value == "")
{
alert("Please enter a value for the Email field.");
theForm.email.focus();
isValid = false;
}

if (isValid && theForm.nPassword.value == "")
{
alert("Please enter a value for the Password field.");
theForm.nPassword.focus();
isValid = false;
}

if (isValid && theForm.nPassword.value != theForm.nConfirm.value)
{
alert("The two passwords are not the same.");
theForm.nConfirm.focus();
isValid = false;
}
if (isValid){theForm.submit()}
}

</Script>
</Head>
<Body>
<form method="post" action="register.php" name="register">
First Name: <input type="text" name="firstname" ><br >
Last Name: <input type="text" name="lastname" ><br >
User Name: <input type="text" name="username" ><br >
Email: <input type="text" name="email" ><br >
Password: <input type="password" name="nPassword" ><br >
Confirm Password: <input type="password" name="nConfirm" ><br >
<br>
<a href="#" onclick="checkForm(document.forms.register)" class="nav">Register</a>
</form>
</Body>
</HTML>[/CODE]
Copy linkTweet thisAlerts:
@dave37authorOct 10.2005 — Give this a try:
<i>
</i>&lt;a href="javascript:[color=royalblue]void([/color]document.register.submit()[color=royalblue])[/color]" class="nav"&gt;Register&lt;/a&gt;
[/QUOTE]


I tired this and it does not work. It will only validate the password field? The rest of the fields are ignored.
Copy linkTweet thisAlerts:
@dave37authorOct 10.2005 — Dave:

"password" and "confirm" are reserved words, and shouldn't be used as field names.
[/QUOTE]


Thanks for pointing that out. I changed them as you did and it did not help the situation. I'm not sure if you were trying to help me with my submit problem or if you were simply pointing this out to me. But i think the register.php script the form gets submitted too needs the password and confirm fields and not nPassword and nConfirm.
Copy linkTweet thisAlerts:
@UltimaterOct 11.2005 — Try removing the "langauge" attribute from both your SCRIPT and your FORM.
Copy linkTweet thisAlerts:
@dave37authorOct 11.2005 — Try removing the "langauge" attribute from both your SCRIPT and your FORM.[/QUOTE]

I tired that. This also does nothing.

I also figured out its not validating at all. I posted above that it validates the password field only.

Thats not entirely true. when the form is submitted, it is submitted to register.php. Register.php validates that field and checks to make sure the min and max password length is ok. so the javascript is not being called at all.

Thank you all for your time thus far.
Copy linkTweet thisAlerts:
@MjhLkwdOct 11.2005 — Dave:

I posted complete code, not just the note about reserved words. It worked for me locally. I guess you're not interested.
Copy linkTweet thisAlerts:
@dave37authorOct 11.2005 — Dave:

I posted complete code, not just the note about reserved words. It worked for me locally. I guess you're not interested.[/QUOTE]


Sorry, I didn't realize that. I copied what you did and it works! Thank you very much. But there is one little problem. If i hit enter instead of clicking the submit button, it skips the validation process. Is there a fix to this?
Copy linkTweet thisAlerts:
@MjhLkwdOct 11.2005 — Dave:

Not that I can see. Even if you gave "focus" to the "submit button" onload, whenever someone started typing in the fields, that field has "focus." Your whole problem from the start is that you don't want to use an actual Submit button. Probably because you don't like its appearance. But, you can change the appearance of regular HTML buttons by using CSS.

You can give the button an ID, like this:

<input type='submit' value='Register' ID='submitBtn'>

Then in the Head section, place a Style section:

<Style type="text/css">

#submitBtn {color:blue;

font-size:14pt;

background-color:lightyellow;

}

</Style>

and so on...
Copy linkTweet thisAlerts:
@MjhLkwdOct 11.2005 — Dave:

Here's your code using a submit button, with its appearance changed by CSS.

[CODE]<HTML>
<Head>
<Style type="text/css">

#submitBtn {color:blue;
font-size:14pt;
background-color:lightyellow;
}

</Style>
<Script type="text/javascript">

function checkForm(theForm){

if (theForm.firstname.value == "")
{
alert("Please enter a value for the First Name field.");
theForm.firstname.focus();
return false;
}

if (isValid && theForm.lastname.value == "")
{
alert("Please enter a value for the Last Name field.");
theForm.lastname.focus();
return false;
}

if (isValid && theForm.username.value == "")
{
alert("Please enter a value for the User field.");
theForm.username.focus();
return false;
}

if (isValid && theForm.email.value == "")
{
alert("Please enter a value for the Email field.");
theForm.email.focus();
return false;
}

if (isValid && theForm.nPassword.value == "")
{
alert("Please enter a value for the Password field.");
theForm.nPassword.focus();
return false;
}

if (isValid && theForm.nPassword.value != theForm.nConfirm.value)
{
alert("The two passwords are not the same.");
theForm.nConfirm.focus();
return false;
}
}

</Script>
</Head>
<Body>
<form method="post" action="register.php" name="register" onsubmit="return checkForm(this)">
First Name: <input type="text" name="firstname" ><br>
Last Name: <input type="text" name="lastname" ><br>
User Name: <input type="text" name="username" ><br>
Email: <input type="text" name="email" ><br>
Password: <input type="password" name="nPassword" ><br>
Confirm Password: <input type="password" name="nConfirm" ><br>
<br>
<input type='submit' value='Register' id='submitBtn'>
</form>
</Body>
</HTML>[/CODE]
×

Success!

Help @dave37 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...