473,412 Members | 4,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Form validation

132 100+
Hi guys,

I'm having difficulties with a form I want to validate.
Below I have pasted an excerpt of my code but my problem lies here:
Expand|Select|Wrap|Line Numbers
  1. if VNom = "" or VEmail = "" or VComment = "" then
  2. response.redirect("react.asp")
  3. end if
  4.  
Now this code works without any problem. The only thing is I don't want him to reload the page when a field hasn't been filled in but I want to have a messagebox popping up stating that they haven't filled in that field.

This way I prevent users from retyping everything they already typed before trying to send the form.

Now I tried a few options already where I use a piece of javascript to have an alert popup but when I test it, it still opens a new page.

So I'm completely blocked.
Please note that I'm novice to ASP so this is code that I copied from here and there so it might not be "real asp".

Below is the Send part of the code.
Thanks for your help.

Expand|Select|Wrap|Line Numbers
  1. if action = "Send" then
  2.  
  3. login = request.form("login")
  4. VNom = escstring(request.form("nom"),"'")
  5. VEmail = escstring(request.form("email"),"'")
  6. VComment = escstring(request.form("msgText"),"'")
  7.  
  8.     if VNom = "" or VEmail = "" or VComment = "" then
  9.     response.redirect("react.asp")
  10.     end if
  11.  
  12. Ye = datepart("yyyy",now())
  13. Mm = datepart("m",now())
  14. if Mm < 10 then
  15. M = "0" & Mm
  16. else
  17. M = Mm
  18. end if
  19.  
  20. Dd = datepart("d",now())
  21. if Dd < 10 then
  22. D = "0" & Dd
  23. else
  24. D = Dd
  25. end if
  26.  
  27. SQLQuery= "INSERT INTO Table1 (login, nom, email, comment, jour, mois, annee) VALUES( '" &login& "','" &VNom& "','" &VEmail& "','" &VComment& "','" &D& "','" &M& "','" &Ye& "')"
  28. OBJdbConnection.Execute SQLQuery
  29.  
  30. response.redirect("react.asp")
  31.  
Feb 25 '09 #1
8 1254
jhardman
3,406 Expert 2GB
Your javascript approach is on the right track. What you need to change is remove the form submitting behavior - nothing the user can do should be able to post the form. Instead, the button he clicks should validate via javascript, then submit IF it validates. Does this make sense?

Jared
Feb 26 '09 #2
Cainnech
132 100+
Hi Jared,

It absolutely makes sense. I have tried this actually. The problem is that in order to submit I have to assign an action to it as well.

So when I press the submit-button appearantly the buttons sends an action along which I can't simulate.

Expand|Select|Wrap|Line Numbers
  1. <input type="submit" name="action" class="button" value="Send">
  2.  
This is the code for the submitbutton.

The name="action" is of importance and also the value="Send" also. Because it is that value that causes the input to be posted.
Expand|Select|Wrap|Line Numbers
  1. if action = "Send" then 
  2.  
So in javascript I can post a form by means of document.form.submit() but here I don't have a clue how I can take that action along.

The action in the form is set now to react.asp
Expand|Select|Wrap|Line Numbers
  1. <form action="react.asp" method="post" name="reactform">
  2.  
So what I also already tested was to change the action to react.asp?action=Send but without any luck unfortunately.
Feb 26 '09 #3
jhardman
3,406 Expert 2GB
Let me ask a javascript expert to weigh in, but here is a simple work-around:

Do not provide a submit button in the form, instead add a button below the form that calls the validate function and then submits the form. The downside of this approach is you can't just hit 'enter' to submit, but this approach is often used. Another approach is to disable the submit button at first and validate after each input loses focus. When the form validates you just need to enable the submit button.

You could also take a pure ASP approach and submit to the same page. If any of the form variables are blank you show the form (but use any submitted values to fill the form as much as possible - see code below). That way, on the first time through the user sees the form (because obviously there is no form data supplied) but if he submits without completing the form, he sees the form again partially filled out. If he submits it finished, handle the data and redirect.

Jared
Feb 26 '09 #4
Cainnech
132 100+
Hey Jared,
I never thought of the disable function actually but it´s a good idea.
I'll look into it.
Feb 26 '09 #5
acoder
16,027 Expert Mod 8TB
One simple way to validate forms using JavaScript is to use the onsubmit event:
Expand|Select|Wrap|Line Numbers
  1. <form action="react.asp" method="post" name="reactform" onsubmit="return validate()">
then in the validate() function, return true or false:
Expand|Select|Wrap|Line Numbers
  1. function validate() {
  2.   if (condition not met) {
  3.     alert(...);
  4.     field.focus();
  5.     return false;
  6.   } else ...
  7.   // and so on...
  8.   // finally, if validation passes
  9.   return true;
  10. }
Feb 27 '09 #6
jhardman
3,406 Expert 2GB
I've moved this thread to the javascript forum since it appears the solution you are pursuing is a javascript sol'n. Let me know if this is OK.

Jared
Feb 27 '09 #7
Cainnech
132 100+
Hey Jared, the move is ok.

Acoder, a big thanks to you because this did the trick.
Fantastic!

Thanks mate!
Feb 28 '09 #8
acoder
16,027 Expert Mod 8TB
You're welcome! Just a word of warning though: do not depend on JavaScript validation. It's there as a convenience. The real validation should still take place on the server-side (ASP code).
Feb 28 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now...
4
by: TG | last post by:
I have a validation form that must behave differently based on the results of a PHP validation check. I have a post command at the top of my form that calls itself. I don't leave the form when...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
10
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the...
5
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.