/    Sign up×
Community /Pin to ProfileBookmark

desperately need help with javascript form

Hi, I’m really new to javascript, just started yesterday. I have a simple login form with a radio button, a username text box, and a password text box. I think I have the javascript for validating the form, it gives an alert when a user does not select a radio button and fill in the other 2 fields.

If the user selects a radio button and fill in the other 2 fields,I want them to get a confirmation asking if they want to login. I think I have that also. What I do not know how to do is:

If they click the “OK” button in the confirmation box, can I get an alert that captures what they are submitting as the login, shows it in the alert then give them the option to click “OK” before submitting the form.

Here is the javascript and HTML I’ve got so far. Any help and suggestion would be greatly appreciated.

Thanks. kky

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>Form 1</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<link href=”form.css” rel=”stylesheet” type=”text/css”>

<script language=”javascript” type=”text/javascript”>
<!–

function isValidRadio(radio)
{
var valid = false;
for (var i = 0; i < radio.length; i++)
{
if (radio[i].checked)
{
return true;
}
}
alert(“Please enter all fields.”);
return false;
}

function isNotEmpty(elem)
{
var str = elem.value;
var re = /.+/;
if(!str.match(re))
{
alert(“Please enter all fields.”);
return false;
}
else
{
return true;
}
}

function validate(form)
{
if (isValidRadio(form.status))
{
if (isNotEmpty(form.username))
{
if (isNotEmpty(form.password))
{
window.confirm(“Do you want to login?”);
return true;
}
}
}
return false;
}

//–>
</script>

</head>

<body>
<form name=”frm1″ onSubmit=”return validate(this)”>
<table>
<tr>
<td colspan=”2″><h1>Form 1 </h1>
<table class=”border”>
<tr>
<td colspan=”2″><h2>Select your UVic Status</h2></td>
</tr>
<tr>
<td>
<input type=”radio” name=”status” value=”student”>&nbsp;Student
</td>
<td>
<input type=”radio” name=”status” value=”staff”>&nbsp;Staff
</td>
</tr>
<tr>
<td>Username:
</td>
<td>
<input type=”text” name=”username”>
</td>
</tr>
<tr>
<td>Password:
</td>
<td>
<input type=”password” name=”password”>
</td>
</tr>
<tr>
<td colspan=”2″>
<input type=”image” src=”login.gif” name=”imgLogin”>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>

</html>

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@PittimannMay 10.2004 — Hi!

Wouldn't it be enough to display these things in the confirm box you already have??

Btw: hitting cancel will submit the form using your code.

I would suggest: display the stuff in the already existing confirm, if user hits ok=>form will be submitted; if user hits cancel, he will stay on the page with the form.

What do you think about that?

Cheers - Pit
Copy linkTweet thisAlerts:
@newtojavascriptauthorMay 10.2004 — Hi, thanks for the quick response.

I prefer if they can have the intermediary confirm box then the alert box with their submission choices. But if it's not possible, I'm OK with it displaying in the confirm box. However, I still don't know how to capture the radio button choice and the username text in the confirm box to show the user what they are submitting.

I also have tried everything to not have the form submit when the user clicks "Cancel", but I can't seem to get it to not submit. Any help on that would be great.

Thanks again. kky
Copy linkTweet thisAlerts:
@PittimannMay 10.2004 — Hi!

Just replace your function validate() with this:[code=php]function validate(form){
if (isValidRadio(form.status)&&isNotEmpty(form.username)&&isNotEmpty(form.password)){
for (var i=0; i<form.status.length;i++){
if (form.status[i].checked==true){
var stat=form.status[i].value;
}
}
if (window.confirm("You entered the following data:nUsername: "+form.username.value+"nPassword: "+form.password.value+"nStatus: "+stat+"nDo you want to login?")){
return true;
}
else{
return false;
}
}
return false;
}
[/code]

I don't think, it is nice to the users to have an alert AND a confirm box (too much clicking buttons); if you really need it, just post again...

Cheers - Pit
Copy linkTweet thisAlerts:
@newtojavascriptauthorMay 10.2004 — Hi, thanks, that works great. I'm learning lots.

I think your suggestion about not using a confirm then an alert box is a good one. However, because I was tearing my hair out all day yesterday trying to find a way to do that, out of curiousity, would it be possible to show me how to get the same results but with a confirm box in between.

Thanks, I really appreciate the help. kky
Copy linkTweet thisAlerts:
@PittimannMay 10.2004 — Hi!

Like this?
[code=php]
function validate(form){
if (isValidRadio(form.status)&&isNotEmpty(form.username)&&isNotEmpty(form.password)){
for (var i=0; i<form.status.length;i++){
if (form.status[i].checked==true){
var stat=form.status[i].value;
}
}
if (window.confirm("You entered the following data:nUsername: "+form.username.value+"nPassword: "+form.password.value+"nStatus: "+stat+"nDo you want to proceed?")){
if (window.confirm("Do you want to login?")){
return true;
}
}
else{
return false;
}
}
return false;
}
[/code]

Cheers - Pit
Copy linkTweet thisAlerts:
@newtojavascriptauthorMay 10.2004 — Hi, thanks again. I was totally trying the wrong approach before. I don't think I would ever have gotten it.

I tried playing around with changing it to the "Do you want to login" confirm box first then the "You entered the following data:" confirm box, but I seem to keep breaking it.

Can you show me also how to switch those 2 around. This is what I did, but it no longer works. What did I do wrong.

Thanks so much. kky

function validate(form){

if (isValidRadio(form.status)&&isNotEmpty(form.username)&&isNotEmpty(form.password)){

for (var i=0; i<form.status.length;i++){

if (form.status[i].checked==true){

var stat=form.status[i].value;

}

}

if (window.confirm("Do you want to login?")){

return true;

if (window.confirm("You entered the following data:nUsername: "+form.username.value+"nPassword: "+form.password.value+"nStatus: "+stat+"nDo you want to proceed?")){

}

}

else{

return false;

}

}

return false;

}
Copy linkTweet thisAlerts:
@PittimannMay 10.2004 — Hi!

Just put the "return true;" after the SECOND if (window.confirm("..."){

Cheers - Pit
Copy linkTweet thisAlerts:
@newtojavascriptauthorMay 10.2004 — Hi, awesome. That really helped me understand quite a few things I was having so much problems with yesterday.

Thanks so much for help and the patience.

kky
Copy linkTweet thisAlerts:
@PittimannMay 10.2004 — Hi!

I am glad if I could help. You're welcome! ?

Cheers & good luck - Pit
×

Success!

Help @newtojavascript 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.28,
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,
)...