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

validation of date and set focus problem

Hi,

I am trying to validate these values, this seems to work fine for the
phone number and name but I am trying to get the program to fail to
submit and set the focus on the date when 2006 is selected and
submitted.

Thanks in advance for any help.
<HTML>
<head>
<link rel="stylesheet" type="text/css" href="test.css"/>
<script language="JavaScript">
<!------------------------------------GENERIC
TEST-------------------------------------------------------------->
function genericTest(anyRE,title,stringToTest) {
if(anyRE.test(stringToTest)) {
return true;
} else {
window.alert("The " + title +" format of:\n" + stringToTest + "\nwas
not recognised.");
return false;
}
}

<!------------------------------------CHECK
WHOLE-------------------------------------------------------------->
function checkWhole() {
/*
if OK variable is false, skip subsequent tests so user can
correct entries one at a time in the same order as on the form
*/
OK = checkFullName(document.form1.CCName.value);
OK = checkDate(document.form1.CCEYear.value);
if ( OK ) OK = genericTest(phoneRE,"telephone
number",document.form1.phone.value);

return OK; // OK false if any ONE (or more) test fails
}

<!------------------------------------CHECK FULL
NAME-------------------------------------------------------------->
// Reject full name if less than 5 characters
function checkFullName (fn) {
if(fn.length < 5) { // reject missing or full name less than 5
characters
window.alert("The full name format:\n" + fn + "\nwas not recognised
(missing or too short).");
return false;
} else {
return true;
}
}

function checkDate(val){
if(val==2006){
window.alert("Invalid Year");

return false;
} else {
return true;
}
}

/*PHONE RE*/
phoneRE = /^(\+|\s)*\d(\d|\s)*(\(\s*\d(\d|\s)*\)|(\d|\s)*)(\d |\s)+(x\s*\d(\d|\s)*|ext\s*\d(\d|\s)*)?$/i;
/*friendly_emailRE*/

friendly_eMailRE = /^\s*[\w\-\.]+@([a-z0-9]+\.){1,3}[a-z]{2,6}\s*$/i;

/*this e-mail address RE is COMPUTER friendly because it rejects all
spaces*/
eMailRE = /^[\w\-\.]+@([a-z0-9]+\.){1,3}[a-z]{2,6}$/i;

</script>
</head>
<BODY>
<IMG SRC="logo.gif"><BR>
<H1>Order Form</H1>
<HR>
<FORM NAME="form1" method="post"
action="https://media.paisley.ac.uk/cgi-davison/shopMail.cgi"
onSubmit="return checkWhole();">
<table border=0 ><tr>

</tr>
<th>Phone</th><th><INPUT TYPE="TEXT" NAME="phone" SIZE=25
onChange='dummy=genericTest(phoneRE,"telephone
number",this.value);'></th>
</tr>
<tr>
<th>
</tr>
<tr>
<th>CCName</th><th><INPUT TYPE="TEXT" NAME="CCName" SIZE=30
onChange='dummy=checkFullName(this.value);'></th>

<tr>
<th>Card ExpiryYear</th>
<th><select name="CCEYear" tabindex="1" ;>
<option value= "invalid" SELECTED > Select Year</option>
<option vale="2005" >2005</option>
<option vale="2006" >2006</option>
<option vale="2007" >2007</option>
<option vale="2008" >2008</option>

</select></th>
</table>

<input type="hidden" name="recipient" value="ab*@hotmail.com">
</SCRIPT>
</TABLE>
<INPUT TYPE="SUBMIT" VALUE="Submit Order">
</FORM>
</BODY>
</HTML>
Jul 23 '05 #1
6 4814
"MickG" <mi*****@hotmail.com> wrote in message
news:1a*************************@posting.google.co m...
Hi,

I am trying to validate these values, this seems to work fine for the
phone number and name but I am trying to get the program to fail to
submit and set the focus on the date when 2006 is selected and
submitted.

Thanks in advance for any help.


"value=" not "vale=".

<option vale="2005" >2005</option>
<option vale="2006" >2006</option>
<option vale="2007" >2007</option>
<option vale="2008" >2008</option>
Jul 23 '05 #2
thanks, sometimes you need another pair of eyes, but i'm still trying
to get the focus() working after the validation.

again, any help would be greatly appreciated

Jul 23 '05 #3
<mi*****@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
thanks, sometimes you need another pair of eyes, but i'm still trying
to get the focus() working after the validation.

again, any help would be greatly appreciated


Change
OK = checkDate(document.form1.CCEYear.value);
to
OK = checkDate(document.form1.CCEYear);
then change
if(val==2006) {
to
if(val.value==2006) {
val.focus();

Also, your "function checkWhole()"
doesn't fo what you say it will do; specifically,

/*
if OK variable is false, skip subsequent tests so user can
correct entries one at a time in the same order as on the form
*/

What's with all the references to:
onchange="dummy=.." ?
Jul 23 '05 #4
MickG wrote:
Hi,

I am trying to validate these values, this seems to work fine for the
phone number and name but I am trying to get the program to fail to
submit and set the focus on the date when 2006 is selected and
submitted.

Thanks in advance for any help.
<HTML>
<head>
<link rel="stylesheet" type="text/css" href="test.css"/>
<script language="JavaScript">
Language attribute for script elements is depreciated, use type:

<script type="text/javascript">

Do not post code with tabs. Replace all tabs with 2 or 4 spaces and
manually wrap lines at about 70 characters to prevent automatic
wrapping which introduces errors into your code.

Your code and script is riddled with errors, I'll point out a few but
not all. Run your code through a validator (there's a link to the
free w3c validator below):

<URL:http://validator.w3.org/>

<!------------------------------------GENERIC
TEST-------------------------------------------------------------->
This is an HTML comment and will cause an error in JavaScript.

/* --------- GENERIC TEST ---------*/
function genericTest(anyRE,title,stringToTest) {
if(anyRE.test(stringToTest)) {
return true;
} else {
window.alert("The " + title +" format of:\n" + stringToTest + "\nwas
not recognised.");
return false;
}
}
Presuming the 'if' logic is OK, it can be more simply written as:

function genericTest(anyRE,title,stringToTest) {
if( !anyRE.test(stringToTest) ) {
alert('The ' + title +' format of:\n'
+ stringToTest + '\nwas not recognised.');
return false;
}
}

There is no need to explicitly return true, that will happen anyway if
the script successfully completes.

<!------------------------------------CHECK
WHOLE-------------------------------------------------------------->
/* -------- CHECK WHOLE -------- */

function checkWhole() {
/*
if OK variable is false, skip subsequent tests so user can
correct entries one at a time in the same order as on the form
*/
OK = checkFullName(document.form1.CCName.value);
OK = checkDate(document.form1.CCEYear.value);
If checkFullName returns false, then checkDate returns true, you will
not detect the 'invalid' date because OK will be reset to true.

Returning errors one at a time like this is very tiresome for users,
better to build a single message and write all errors to the page so
they can be fixed, but I'll leave that up to you.
if ( OK ) OK = genericTest(phoneRE,"telephone
number",document.form1.phone.value);

return OK; // OK false if any ONE (or more) test fails
All of the above can be replaced with:

if (! checkFullName(document.form1.CCName.value) {
alert("Fix full name");
return false;
}
if (! checkDate(document.form1.CCEYear.value) {
alert("Fix date");
return false;
}
if (! genericTest(phoneRE,"telephone number",
document.form1.phone.value) {
alert("Fix phone number");
return false;
}

Note that automatic wrapping breaks your code even more.
}

<!------------------------------------CHECK FULL
NAME-------------------------------------------------------------->
/* -------- CHECK FULL NAME -------- */
// Reject full name if less than 5 characters
function checkFullName (fn) {
if(fn.length < 5) { // reject missing or full name less than 5
characters
window.alert("The full name format:\n" + fn + "\nwas not recognised
(missing or too short).");
return false;
} else {
return true;
}
No need for the 'else', just return false if your test returns true
and if otherwise, the script will successfully complete and return
true.
}

function checkDate(val){
if(val==2006){
window.alert("Invalid Year");

return false;
} else {
return true;
}
Same here.
}

/*PHONE RE*/
phoneRE = /^(\+|\s)*\d(\d|\s)*(\(\s*\d(\d|\s)*\)|(\d|\s)*)(\d |\s)+(x\s*\d(\d|\s)*|ext\s*\d(\d|\s)*)?$/i;
/*friendly_emailRE*/

friendly_eMailRE = /^\s*[\w\-\.]+@([a-z0-9]+\.){1,3}[a-z]{2,6}\s*$/i;

/*this e-mail address RE is COMPUTER friendly because it rejects all
spaces*/
eMailRE = /^[\w\-\.]+@([a-z0-9]+\.){1,3}[a-z]{2,6}$/i;


I have no idea if these RegExps work, I didn't get your page to work,
look like they are not appropriate. Search this group for better
expressions, I think some were posted recently (within the last
month).

If you are ever going to test formats, you *must* tell the user what
format you expect or they are simply guessing in the dark. I also
don't see why these have to be global variables, surely each needs to
be used just once inside a single test module?

</script>
</head>
<BODY>
<IMG SRC="logo.gif"><BR>
<H1>Order Form</H1>
<HR>
<FORM NAME="form1" method="post"
action="https://media.paisley.ac.uk/cgi-davison/shopMail.cgi"
onSubmit="return checkWhole();">
<table border=0 ><tr>

</tr>
An empty row?
<th>Phone</th><th><INPUT TYPE="TEXT" NAME="phone" SIZE=25
onChange='dummy=genericTest(phoneRE,"telephone
number",this.value);'></th>
th tags outside a row? th is meant for a header row, you've used it
like a general cell tag. Use td unless you really mean th.
</tr>
Closing tr without an opening tr.
<tr>
<th>
</tr>
No closing th? Only one cell with the above row (probably) has two
cells? No span attribute?
<tr>
<th>CCName</th><th><INPUT TYPE="TEXT" NAME="CCName" SIZE=30
onChange='dummy=checkFullName(this.value);'></th>
You assign the return value of checkFullName to the global
variable 'dummy'; but don't use dummy anywhere. If there is no need
for dummy, why not:

<td>CCName</td>
<td><INPUT TYPE="TEXT" NAME="CCName" SIZE="30" onChange="
checkFullName(this.value);
">
</td>

Doing validation 'onchange' is not nice as nothing happens until the
user clicks on the next input (i.e. the 'changed' input loses focus).
They've moved on, but the validation drags them back to an input they
thought they'd completed.

Not only that, but they get trapped in an endless loop of alerts
until they enter a valid value - and you don't even give a hint as to
what that might be. Their only other recourse is to kill the
browser.

Better to just do validation on submit.

<tr>
<th>Card ExpiryYear</th>
<th><select name="CCEYear" tabindex="1" ;>
The semi-colon at the end is redundant, it probably wont cause an
error but it isn't needed.
<option value= "invalid" SELECTED > Select Year</option>
<option vale="2005" >2005</option>
<option vale="2006" >2006</option>
<option vale="2007" >2007</option>
<option vale="2008" >2008</option>

</select></th>

More invalid script - no closing tr? Your 'vale' error has already
been pointed out. If the value and the text are the same, just use
the text as the value will be the text (or so the spec claims and
unless testing proves otherwise).

</table>

<input type="hidden" name="recipient" value="ab*@hotmail.com">
</SCRIPT>
</TABLE>
These tags seem way out of place, ditch 'em.
<INPUT TYPE="SUBMIT" VALUE="Submit Order">
</FORM>
</BODY>
</HTML>


To get the focus back on your 'CCEYear' form element:

if (document.form1.CCEYear.blur()) {
document.form1.CCEYear.blur();
}

That is not the full extent of your errors, but I don't have time to
find or fix them all.

I suggest you start by including one input and one script and get
that working, then build up slowly until it all works and validates.
--
Fred
Jul 23 '05 #5
thanks for the help, I'll get to it.

Michael

Jul 23 '05 #6
mi*****@hotmail.com wrote:
thanks for the help, I'll get to it.

Michael


Here is a head start - I missled you on the return true thing. If
you don't sepecify a value, functions that complete return
'undefined', which is not false (duh!) unless you specify otherwise.
The script below provides a skeleton to get going, use what you like.

Lightly commented, tested in Safari and Firefox on Mac OS X.

<HTML>
<head><title>stuff</title></head>
<BODY>

<script type="text/javascript">
function validateForm(f){
var ph = f.phone.value;
var ccn = f.CCName.value;
var yr = f.CCEYear.value;
var msg = [];
var t;

// Test phone number
// Format is 8 digits
if ( (t=testPH(ph)) && 'undefined' != typeof t ) {
msg.push(t);
}

// Test credit card name
// Must be more than 5 characters without spaces
if ( (t=testCCN(ccn)) && 'undefined' != typeof t) {
msg.push(t);
}

// Test year
// must not be 2006 or not a number (see note below)
if ( (t=testYR(yr)) && 'undefined' != typeof t) {
msg.push(t);
}

// If we got any errors, write them & return false
if ( 0 != msg.length ){
writeMsg(msg.join('<br>'));
return false;
}
}

function testPH(p) {
// remove spaces, check if 8 digits and 8 long
// you may want a different test here
p = p.replace(/\s*/g,'');
if (!/\d{8}/.test(p) || 8 != p.length ) {
return '<b>Phone number must be 8 digits</b>';
}
}

function testCCN(c) {
// remove spaces, check if 5 characters of any kind
// you may want a better test here
var x = c.replace(/\s*/g,'');
if (x.length < 5) {
return '<b>Name must be at least 5 characters,'
+' not counting the spaces</b>';
}
}
function testYR(y) {
// check if y is 2006 or not a number
// Note: only use isNaN here because value is from conrolled list
// Otherwise more robust validation is required.
if ( 2006 == y || isNaN(y) ) {
return '<b>Year can\'t be ' + y + '</b>'
}
}

function checkForm(f) {
// Run validate from a button rather than onclick
if ('undefined' == typeof validateForm(f)){
var txt = 'Values are all OK';
writeMsg(txt)
}
}

function clearErr(f){
// Just clears error messages
writeMsg('');
}

function writeMsg(m){
// Generic write function. If getElementById not supported,
// Presents error in an alert after repalcing <br> with \n
// and removing all other markup
if (document.getElementById) {
document.getElementById('eSpan').innerHTML = m;
} else {
alert(m.replace(/<br>/g,'\n').replace(/<[^<>]*>/ig,''));
}
}

</script>
<H1>Order Form</H1>
<HR>
<FORM NAME="form1" action="" onSubmit="return validateForm(this);
return false;">
<table border="0">
<tr>
<td>Phone</td>
<td>
<INPUT TYPE="TEXT" NAME="phone" SIZE="25"
value="1234 5678"><br>
<i>nnnn nnnn</i>
</td>
</tr>
<tr>
<td>CCName</td>
<td>
<INPUT TYPE="TEXT" NAME="CCName" SIZE="30"
value="asdfasdf"><br>
<i>Must be more than 5 characters</i>
</td>
</tr>
<tr>
<td>Card ExpiryYear</td>
<td>
<select name="CCEYear">
<option SELECTED value="default">Select Year</option>
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
</select>
</td>
</tr>
</table>
<INPUT TYPE="button" VALUE="check form"
onclick="checkForm(this.form);">
<INPUT TYPE="reset" onclick="clearErr(this.form);
this.form.reset();">
<INPUT TYPE="SUBMIT" VALUE="Submit Order">
</FORM>
<span id="eSpan"></span>
</BODY>
</HTML>
--
Fred
Jul 23 '05 #7

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

Similar topics

3
by: Marc Llenas | last post by:
Hi there, I'm stuck on a validation function for a form and I cannot figure out what the problem is. The page is in ASP. Any ideas? The function being called is: <script...
0
by: Steve | last post by:
Hello I have several controls on a form linked to a dataset. Two of the controls can be used to populate one field in the dataset (i.e. by selecting an item from a drop down list to get an id,...
4
by: Mad Scientist Jr | last post by:
i am trying to set focus to a specific control depending on the outcome of a validator control and it is not working. none of these methods are working to setfocus: 1....
2
by: Tim Frawley | last post by:
Source code attached indicates my problem with validation and a button bar save button. Fill the Textbox with some text then tab off the control. The message box will display the text in the...
1
by: chuaby | last post by:
Hi I have a generic form to keep data. I.e, date, number, string all are stored as TEXT in different rows in a same table. i would like to validate that the date is in a correct format. The...
2
by: John Smith | last post by:
Hello, I have a VB.NET application with a Windows form that have several textboxes fields where I have dates entered. I would like to do a date validation check after the the field is updated, so...
4
by: yatin | last post by:
hi, friend. i have a problem in image validation plz check it. i have to include a image validation in foam 1. but i sending a details through form2. But when i include a image validation in foam1,...
15
by: MKO | last post by:
Hi guys I'm having a little trouble with form validation, I can get the entire form to validate without problems (i.e. when all fields set to required & valid data is entered) but can't get it to...
3
by: satishknight | last post by:
Hi, Can some one tell me how to change the validation sequence for the code pasted below, actually what I want it when any one enters the wrong login information (already registered users) then it...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...

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.