Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to validate a form and then insert into db if ok. stuck again!

Status
Not open for further replies.

andybid

Vendor
Dec 7, 2010
23
0
0
GB
hi

Ok, I have aform with userrname, password, password repeat and email fields.

I need to check the inputs client side just to make sure they are done right.

I have onChange utilized for this but my problem is that the form can still be sent if the user clicks the button.

If the user fills out the email and clicks the button it bypasses the onchange fuction on the email completely.

How can I resolve this? I'd like for the button to either not activate unless all fields are ok or when clicked to run the fields through the function 'form_validate' (which is all the functions in one that I was using before the onchange) and then to submit if it runs ok through that.

I've tried many things over the last 24 hours, too many variations to list. I am learning as I go so it may be simple and I missed it.

the full code is at


running at

(Youll see the problem if you just hit the button at anytime - it will bypass all my on-change events.



Thank you all in advance


Thank you
 
Your submit button does nothing at all to validate. It simply submits regardless of what happens with your inputs. While your other inputs may validate if its right or wrong, they also do nothing to prevent submission should something not be correct.

Normally one runs a function on the onSubmit event of the actual form, or the onClick event of the button to verify that everything is correct prior to actually submitting the form. If something is found to be wrong, then the function would return a false value to inhibit the submitting of the form.

Now if you are using ajax actually submitting the form defeats the entire purpose of using Ajax. You want to use Ajax to avoid having to submit a page, and in turn expedite the processing a bit. But if you submit the page, and then run the ajax, then there's no point. As you are basically increasing the wait time uselessly.


Anyway, to stop the submission should something not be right, use return values set to false:

Code:
<input type="submit" value="continue" onClick="[red]return [/red]validate(this.form);">

[green]Or[/green]


<form action="..." method="POST" onSubmit="[red]return[/red] validate(this);">

Then in your function you check what you need to, and hould something be wrong return a false value

Code:
function validate(...){
...

if(somevar!=something){
alert('this is wrong');
return [red]false[/red];
}

if(pass1!=pass2){
aler('passwords not the same');
return [red]false[/red];
}

}



NOTE: The unparsed PHP makes it very hard to actually see
what's going on. always try posting the end product, and not the server side code.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
yep, Think I get your point about posting to a diffent page with ajax to do the work.

I have, since posting this, intergrated the inserting into db into the same page using submit. The bit that was getting me was how to stop it doing it if wrrong input.

so the onClick function will run before the form action runs yes?

I dont quiye understand how the return of true/false stops the form action but do I take it it does?

I will try and get my head around this. I am still using dreamweaver to write the database functions (plus got used to its color highlighting now)

I will give this a shot and spend a few hours over the next 2 days getting to grips with it (Can't do it now I am gettin grief from other half cos I am 'wasting time on pc again!'.

If I still cant get it, Ill post where I'm too and go from there.

Thanks

 
as simple as that..

my function now has in, and ends like this.

Code:
...................
 else if (eillegalChars.test(email)) {
   		alert ("The email address contains illegal characters.")
		return false;
	} else if (email.search(emailRegEx) == -1) {
          alert("Please enter a valid email address.")
		  return false;
    } else if (pass != pass2) {
		alert ("Passwords don't match")
		return false;
	} else return true

i have
Code:
<form id="user" name="user" method="POST" onSubmit="return validateform()" action="<?php echo $editFormAction; ?>">

in the form and it all works.

It now posts to itself to write to the db b4 moving to next page.

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top