473,472 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Preventing form submission with chainable methods

5 New Member
I got it into my head the other day to develop my own little form validation library. More than anything I just wanted to try out of few things I’ve never done before like chainable methods -- think jQuery’s style of calls: $('#id1').click().fadeOut('slow').

A problem I have ran into though is how to prevent a form from submitting if it fails one of the validation methods. I know how returning false to the form on submission will do the trick but I am simply not sure how to do that with in the current form of my script.

Here is a small chunk of my code.
Expand|Select|Wrap|Line Numbers
  1. FC = function(id){
  2.     var _id = document.getElementById(id);
  3.  
  4.     return {    
  5.         on: function (evt,fnc,useCapture)
  6.         {
  7.             obj = _id;
  8.             if (!useCapture) { 
  9.                 useCapture = false; 
  10.             }
  11.             if (obj.addEventListener){
  12.                 obj.addEventListener(evt,fnc,useCapture);
  13.                 return true;
  14.             } 
  15.             else if (obj.attachEvent) {
  16.                 return obj.attachEvent("on"+evt,fnc);
  17.             }
  18.         }, 
  19.         alphaOnly: function()
  20.         {
  21.             // check for alpha characters only
  22.             return this;
  23.         },
  24.         minLength: function()
  25.         {
  26.             // check for minimum length
  27.             return this;
  28.         },
  29.         formatNum: function()
  30.         {
  31.             // format a number text field
  32.             return this;
  33.         }
  34.     };
  35. };
  36.  
  37. // The following code is how it would be used in its current form.
  38.  
  39. FC('form1').on('submit',function(){
  40.      FC('input1').minLength('3').alphaOnly();
  41. });
  42.  
This code will find the form with the id of 'form1' and then attach an 'onsubmit' event to it and run second line of code when submitted. The second line will look for the element with the 'input1' id and check that the length is at least three characters long and only contains alpha characters.

Obviously this script won’t be very useful unless I can get it to actually stop a form from submitting so if someone could provide some insight into this that’d be great.
Sep 23 '07 #1
2 1360
dickvla
1 New Member
Have you made any progress with this issue. I am also quite curious as to the answer

Dick
Oct 9 '07 #2
dmjpro
2,476 Top Contributor
Have you made any progress with this issue. I am also quite curious as to the answer

Dick
Have a look at this Sample Code.

Expand|Select|Wrap|Line Numbers
  1. <form name="some name" onsubmit = "return validate()">
  2. .
  3. .
  4. </form>
  5.  
Expand|Select|Wrap|Line Numbers
  1. function validate()
  2. {
  3.  var submit_flag = false;
  4.  .
  5. .
  6. .
  7. //some validation code goes here and assign the "submit_flag" either with true or false
  8. return submit_flag;
  9. }
  10.  
Good Luck!

Debasis Jana
Oct 9 '07 #3

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

Similar topics

6
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
8
by: CJM | last post by:
How do people go about preventing the user from submitting a form for a 2nd time? For example, the user submits a form, clicks on the back button, and the submits the form again. I have used...
2
by: Andy Goldstein | last post by:
I have a table where all the TRs have an onClick handler registered. One (and only one) of the rows has 2 text input boxes, where each textbox has an onChange handler registered. Both the onClick...
3
by: shortbackandsides.no | last post by:
I've been having trouble preventing users pressing Enter part way down a form so the incomplete form gets submitted. I came up with a possible solution - the code below seems to work in both...
6
by: nick4soup | last post by:
I have read the CGI FAQ 'How can I avoid users hitting "submit" twice' (on http://www.htmlhelp.org/faq/cgifaq.3.html#19 ) which essentially says you have to detect it at the server, using a...
10
by: bregent | last post by:
I've seen plenty of articles and utilities for preventing form injections for ASP.NET, but not too much for classic ASP. Are there any good input validation scripts that you use to avoid form...
12
by: Mark Rae | last post by:
Hi, See the previous thread Request.Form abuse in this newsgroup... I'm looking for a simple and efficient way to prevent people hijacking the <formtags on my websites and using them to send...
6
by: Oleg Konovalov | last post by:
Hi, I have a Java/JavaScript GUI application where I perform a lot of long DB operations , which takes 5-60 secs to perform. Sometimes user double-clicks the button or just gets impatient and...
12
by: MikeB | last post by:
I created a small website for a friend. On this website he has a contact page where people can send him email. When I wrote this page I checked some tutorial pages and they warned about certain...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.