473,383 Members | 1,737 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,383 software developers and data experts.

Checking the values of two form fields

hi all,

i have a form and would like to check two fields.
if field 1 containes the letter 'c' and the other field contains
'undefined', then an error should apprear ina box on the screen.

can anyone point me a piece of code ?
Thanx,
Gawie.
Jul 23 '05 #1
2 1834
In article <ca**********@ctb-nnrp2.saix.net>,
"Gawie Marais" <gm*****@gam.co.za> wrote:
hi all,

i have a form and would like to check two fields.
if field 1 containes the letter 'c' and the other field contains
'undefined', then an error should apprear ina box on the screen.


This will get you started. You will need to add some style attributes
to get the box. It will only work for one message. To get different
messages to popup in the same place, you will need to dynamically alter
the document text or use multiple hidden divisions. I put up an alter
message for older browsers.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
<head>
<title>Check form fields</title>

<style type="text/css">
..formStyle {position:relative; visibility:hidden;}
</style>

<script type="text/javascript">
var messageText =
"What a suprise! Please correct the error in your ways." +
" When you have the letter c in the name field, you must" +
" have something in the address field.";

function validate()
{

var x = document.forms["myForm"];
var submitOK = true;

alert(".theName.value = " + x.theName.value +
" ... .length = " + x.theName.value.length +
"\n" +
" .theAddress.value= " + x.theAddress.value +
" ... .length = " + x.theAddress.value.length );
if (x.theName.value.indexOf("c") >= 0 &&
x.theAddress.value.length == 0)
{

if (document.getElementById)
{
document.getElementById("errorMsg").style.visibili ty =
"visible";
}
else
{ alert(messageText); }

submitOK = false;

}
else
{

document.getElementById("errorMsg").style.visibili ty =
"hidden";
}

alert("submitOK= " + submitOK);
return submitOK;

}

</script>
</head>
<body>
<p>Please try out our form.</p>
<form name="myForm"
action="http://www.natAValidWebAddress.com"
method="POST"
onsubmit="return validate();">
<p>Name:<br>
<input type="text" name="theName" size="20"><br><br>
Address:<br>
<input type="text" name="theAddress" size="20"><br>
<script type="text/javascript">
//
// Only insert a div if we can change it
// Span didn't work in IE 5.2 for the Mac
//
if (document.getElementById)
{
var insertMessage = "<div id='errorMsg' class='formStyle'>" +
"<p>" + messageText +
"<\/p><\/div>";
document.write(insertMessage);
}
</script>

<p><input type="submit"></p>

</form>

<script type="text/javascript">
// This code needs to be after the form.
validate();
</script>
</body>
</html>
Jul 23 '05 #2
"Gawie Marais" <gm*****@gam.co.za> wrote in message news:<ca**********@ctb-nnrp2.saix.net>...
hi all,

i have a form and would like to check two fields.
if field 1 containes the letter 'c' and the other field contains
'undefined', then an error should apprear ina box on the screen.

can anyone point me a piece of code ?
Thanx,
Gawie.


Untested. You may have to tweak it a tiny bit.
That way, you'll learn something.

Shawn

<script type="text/javascript">
function checkForm(){

var allOkay = true;

if ((document.forms['formName'].txtFred.value == 'jack') &&
(document.forms['formName'].txtBarney.value == 'jill')){
alert('You shouldn't have done that...');
allOkay = false;
}

return allOkay;
}
</script>

In form:

<form id="formName" action="./test.html" method="post"
onsubmit="if(!checkForm(){){return false;}">
<input type="text" id="txtFred" name="txtFred" value="" />
<br/>
<input type="text" id="txtBarney" name="txtBarney" value="" />

</form>
Jul 23 '05 #3

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

Similar topics

3
by: Antoni | last post by:
Hello I wondered if this script seemed correct? The user should enter a userid and password in two text fields, and then pass these values to the method login_user. Could I ask, is there any...
1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
3
by: Jack | last post by:
Hi, I have a form when loaded, retrieves record from an access table. Among other fields there is a check box called FinalUpdate. This is tied to a field in Access of type Yes/No. The form...
4
by: Tom Esker | last post by:
I've got Javascript in a form that adds up all of the numbers in a column of form fields and displays a total. It works great if every field has an initial value of 0, but if any of them are null,...
3
by: User | last post by:
Form A (Main) Text Box 1 Text Box 2 Text Box 3 Form B (Pop-up) Choose a selection for Form A/Text Box 3 Scenario:
6
by: Neil | last post by:
Hi, I have an aspx page with a number of web controls on it and one of these is a cancel button. I want to check the page to see if the user has changed any of the controls, i..e typed some text...
7
by: john.cole | last post by:
I have searched all the groups I can, and I still haven't been able to come up the solution I need. I have the following problem. In my form named sbfrmSpoolList, I am entering a job, spool and...
1
by: geetamadhavi | last post by:
Hi All, I have developed a php applciaiton where a new window is opening on checking the whether valid user orntot how to make that in same window after checking i have die(' not valid user ' ); i...
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.