473,581 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate multiple form records and highlight errors

Red
Hi,

I'm not very familiar with Javascript. I usually leave that kind of
stuff up to Dreamweaver, but i'm starting to need a little more than it
can offer.

I have an asp page which creates a form from a record set. Very simply
it lists items that can be ordered by the customer. The customer simply
enters the required qty for each item and hits submit:

<form action="order.a sp" method="post" name="OrderForm " id="OrderForm" >
While NOT rsOrderFormItem s.EOF
<tr>
<td><%=(rsOrder FormItems.Field s.Item("ITEM_CO DE").Value)%> </td>
<td><%=(rsOrder FormItems.Field s.Item("DESCRIP TION").Value)%> </td>
<td><input name="ORDER_QTY " id="ORDER_QTY" value="0"></td>
<td><%=(rsOrder FormItems.Field s.Item("MAX_ORD ER_QTY").Value) %></td>
</tr>
Wend

As you can see, the last column lists the maximum that can be ordered.
What I would like is some simple client side validation that checks that
the value in ORDER_QTY is a number between 0 and MAX_ORDER_QTY (or empty
just in case the user deletes a value). I need the validation to be
done for each record.

Also, as there can be quite a lot of records on a page, aswell as a
message box it would be nice if the textboxes with errors could be
higlighted. Maybe change the class of the textbox?

Any help or pointers would be greatly appreciated.

Many TIA,

Red.
Jul 23 '05 #1
5 2586
In article <Wk************ ***@newsfe4-gui.ntli.net>, re*@red.com enlightened
us with...
<td><input name="ORDER_QTY " id="ORDER_QTY" value="0"></td>
Input requires a type attribute, I believe. IMNSHO, it should be there even
if not required, for clarity.

As you can see, the last column lists the maximum that can be ordered.
What I would like is some simple client side validation that checks that
the value in ORDER_QTY is a number between 0 and MAX_ORDER_QTY (or empty
just in case the user deletes a value). I need the validation to be
done for each record.

Also, as there can be quite a lot of records on a page, aswell as a
message box it would be nice if the textboxes with errors could be
higlighted. Maybe change the class of the textbox?


Works in theory. Not tested. Check for typos. Beware of copy/paste with word-
wrap.

<script type="text/javascript">
function validateField(f ield, maxValue)
{
if (field && field.value)
{
if (field.value < 0 ||
field.value > maxValue)
{
// invalid data
alert("oops - bad data in "+field.nam e);
if (field.style) field.style.bac kgroundColor="y ellow";
}
}
}
</script>

<form action="order.a sp" method="post" name="OrderForm " id="OrderForm" >
While NOT rsOrderFormItem s.EOF
maxValue = rsOrderFormItem s.Fields.Item(" MAX_ORDER_QTY") .Value
<tr>
<td><%=(rsOrder FormItems.Field s.Item("ITEM_CO DE").Value)%> </td>
<td><%=(rsOrder FormItems.Field s.Item("DESCRIP TION").Value)%> </td>
<td>
<input type="text" name="ORDER_QTY " id="ORDER_QTY" value="0"
onChange="valid ateField(this,< %=maxValue%>);" >
</td>
<td><%=maxValue %></td>
</tr>
Wend

--
--
~kaeli~
Murphy's Law #3020: Quality assurance doesn't.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2
Red
kaeli wrote:
In article <Wk************ ***@newsfe4-gui.ntli.net>, re*@red.com enlightened
us with...
<td><input name="ORDER_QTY " id="ORDER_QTY" value="0"></td>

Input requires a type attribute, I believe. IMNSHO, it should be there even
if not required, for clarity.


Yes, i only simplified the code a bit for this post.

Thanks for the code. Works on a field by field basis, as and when the
user updates textboxes.

The only time this doesn't work is when the user doesn't tab out of the
textbox or clicks elsewhere, but clicks straight on submit button. The
alert shows, but the form still submits.

How could I prevent this happening?

Thanks!

Red.
As you can see, the last column lists the maximum that can be ordered.
What I would like is some simple client side validation that checks that
the value in ORDER_QTY is a number between 0 and MAX_ORDER_QTY (or empty
just in case the user deletes a value). I need the validation to be
done for each record.

Also, as there can be quite a lot of records on a page, aswell as a
message box it would be nice if the textboxes with errors could be
higlighted. Maybe change the class of the textbox?

Works in theory. Not tested. Check for typos. Beware of copy/paste with word-
wrap.

<script type="text/javascript">
function validateField(f ield, maxValue)
{
if (field && field.value)
{
if (field.value < 0 ||
field.value > maxValue)
{
// invalid data
alert("oops - bad data in "+field.nam e);
if (field.style) field.style.bac kgroundColor="y ellow";
}
}
}
</script>

<form action="order.a sp" method="post" name="OrderForm " id="OrderForm" >
While NOT rsOrderFormItem s.EOF
maxValue = rsOrderFormItem s.Fields.Item(" MAX_ORDER_QTY") .Value
<tr>
<td><%=(rsOrder FormItems.Field s.Item("ITEM_CO DE").Value)%> </td>
<td><%=(rsOrder FormItems.Field s.Item("DESCRIP TION").Value)%> </td>
<td>
<input type="text" name="ORDER_QTY " id="ORDER_QTY" value="0"
onChange="valid ateField(this,< %=maxValue%>);" >
</td>
<td><%=maxValue %></td>
</tr>
Wend

Jul 23 '05 #3
In article <1v************ ***@newsfe1-win.ntli.net>, re*@red.com enlightened
us with...

The only time this doesn't work is when the user doesn't tab out of the
textbox or clicks elsewhere, but clicks straight on submit button. The
alert shows, but the form still submits.

How could I prevent this happening?


You can't. Not with JUST handling onchange.
Put an onsubmit handler in to call the function for every element and return
false if any fail.

<form onsubmit="retur n myValidator(thi s)">

<script type="text/javascript">
function myValidator(frm )
{
// for each text element in frm, do
// get value of function check
// if bad, return false, else loop
return true;
}

If you need help with that, let me know.

--
--
~kaeli~
With her marriage, she got a new name and a dress.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #4
Red
kaeli wrote:

Put an onsubmit handler in to call the function for every element and return
false if any fail.

<form onsubmit="retur n myValidator(thi s)">

<script type="text/javascript">
function myValidator(frm )
{
// for each text element in frm, do
// get value of function check
// if bad, return false, else loop
return true;
}

If you need help with that, let me know.


Appreciate your help. From much googling, i've got things half working,
but as I'm not that familiar with javascript, i'd appreciate a little
more help. The following loops through the fields, and highlights the
boxes that fail validation. I see the boxes change colour in the split
second before the form submits. But the form always submits.

What needs to happen is that at the end of the 'for' loop, if any
textboxes have failed the validation, the form should not submit. If
all textboxes passed the validation, then the form can submit OK.

function validateForm()
{
var z=true;
var f = document.forms['frmOrderForm'];
for(i = 0; i < f.elements.leng th; i=i+3)
{
x=parseInt((f.e lements[i+1].value))
y=parseInt((f.e lements[i+2].value))
if((x < 0 || x > y))
{
f.elements[i+1].className = 'frmInvalid';
z=false;
}
else
{
f.elements[i+1].className = 'frmValid';
}
}
if(z==false)
{
alert("Please ammend highlighted fields")
}
return z;
}

<form action="order.a sp" method="post" name="frmOrderF orm"
id="frmOrderFor m" onsubmit="retur n validateForm()" >
Jul 23 '05 #5
Red
To save anyone trying to answer the question below, all sorted now.

Didn't realise that f.elements.leng th includes any buttons on the form.
Changed the line:

for(i = 0; i < f.elements.leng th; i=i+3)

to

for(i = 0; i < (f.elements.len gth - 2); i=i+3)

to cancel out the submit and cancel button, and it worked perfectly!
Red wrote:
kaeli wrote:

Put an onsubmit handler in to call the function for every element and
return false if any fail.

<form onsubmit="retur n myValidator(thi s)">

<script type="text/javascript">
function myValidator(frm )
{
// for each text element in frm, do
// get value of function check
// if bad, return false, else loop
return true;
}

If you need help with that, let me know.


Appreciate your help. From much googling, i've got things half working,
but as I'm not that familiar with javascript, i'd appreciate a little
more help. The following loops through the fields, and highlights the
boxes that fail validation. I see the boxes change colour in the split
second before the form submits. But the form always submits.

What needs to happen is that at the end of the 'for' loop, if any
textboxes have failed the validation, the form should not submit. If
all textboxes passed the validation, then the form can submit OK.

function validateForm()
{
var z=true;
var f = document.forms['frmOrderForm'];
for(i = 0; i < f.elements.leng th; i=i+3)
{
x=parseInt((f.e lements[i+1].value))
y=parseInt((f.e lements[i+2].value))
if((x < 0 || x > y))
{
f.elements[i+1].className = 'frmInvalid';
z=false;
}
else
{
f.elements[i+1].className = 'frmValid';
}
}
if(z==false)
{
alert("Please ammend highlighted fields")
}
return z;
}

<form action="order.a sp" method="post" name="frmOrderF orm"
id="frmOrderFor m" onsubmit="retur n validateForm()" >

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5512
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE, UPDATE, DELETE) which are controlled by a web frontend and the table records are manipulated to control the permissions. Example: The Press...
3
5014
by: Simon Rowe | last post by:
Probably really simple but I cant work it out... I have a list box on a form with a few records in. When I open the form the first record is sort of highlight with a dashed box, when I cursor down the record changes to inverse video and from then on I can sroll up and down the records with the inverse video highlight. I just cant seem to...
11
4510
by: dskillingstad | last post by:
I've been struggling with this problem for some time and have tried multiple solutions with no luck. Let me start with, I'm a novice at Access and I'm not looking for someones help to design my database,just help in getting me pointed in the right direction. I have a database with 8 tables, which from what I have read, cannot be linked...
4
2456
by: Matt Ratliff | last post by:
Hello, I would appreciate any assistance you have with the following problem: I have (as an example) an array of values as follows: arrayvalues=new Array("0001","0003","0005") where each is the value of an option in a select statement: <select id="usertypes" multiple="multiple"> <option value="0033">data1</option>
4
2074
by: ApexData | last post by:
I have a continuous form that has Allow- Add/Edit/Del set to False, so that the form is in View mode only. I have New/Edit/Del buttons in the form header. My New & Edit buttons work as a like. My Del button works great also, but for one record at a time only. In ViewOnly mode, the user is able to highlight a single record to delete...
2
2384
by: Neo Geshel | last post by:
Greetings, I have a form with a telephone field. It is very specific, as it has four text boxes - the country code, area code, prefix and suffix. I can validate each of them individually, but I am stumped as to how to validate them as a group (as one final validation). I need to check to see if all (at one time) are filled or empty. The...
1
2817
by: Muchach | last post by:
Hello, Ok so what I've got going on is a form that is populated by pulling info from database then using php do{} to create elements in form. I have a text box in each table row for the user to enter input. I need to take this user input and put it back into the database. What would be the best method to do this. I can't use a normal post...
1
3986
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. Depends on the pull down list selection, I use script.aculo.us to validate the user input before submit and pass the necessary data, such as contact...
1
2828
by: mbarnhizer | last post by:
Hello All, Trying to figure out how to validate a series of questions on an online test. I am thinking that VB or Javascript is the best route, but your input may make a difference. The site i am working with is using .asp. Their are 30 multiple choice questions. Each will have have 3 or 4 checkboxes where the test taker will choose only...
0
8156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7910
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5681
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.