Re: Screen refreshes after displaying error message

Thursday, September 25, 2014

0 comments
The reason the error message only stays for a mere second is because of the way you have hooked into the form's onsubmit event handler. After the user submits their entries, then if your code detects an error in those entries, the onsubmit event must be explicitly canceled by your code to give the user an opportunity to correct their mistake.

The best way to to do this to change your form tag like this:

   <form action="" id="frmReg" novalidate onsubmit="return process();">

And eliminate this line:

  document.getElementById('frmReg').onsubmit = process;

Finally, change the process() function like this:

function process() {
  'use strict';
  
  var dob;     // retrieve dob value from HTML
  var msg;     // output error message
  var dobDate; // convert dob to date type
  
  dob = document.getElementById('dob');
  msg = '';
  
  if (dob && dob.value) {
    dobDate = new Date(dob.value);
    if (dobDate.getTime()) {
    }
    else // invalid date or invalid input
    {
      msg = 'Invalid date!';
      setText('errMsg', msg);
      return false; // Added by Jal to cancel submit
    }
  } // end if
  return true; // Added by Jal
}  // end process()

The above changes will cancel the submission event if the date is missing or not valid.

Also, you should test this code on all the browsers you expect to support.

Good luck.

On Saturday, September 20, 2014 6:01:16 PM UTC-4, Patrick Wong wrote:
Hello,

I'm creating a registration page that, among other things, displays an error message when an invalid date is entered. The JavaScript I wrote detects the error and displays the error message -- for less than one (1) second -- then the entire page refreshes to display a blank registration page. Why does it do that and how can I get it to stop refreshing?

JavaScript Code:
function setText(elementId, message)
{
 
'use strict';
 
 
if ((typeof elementId == 'string') && (typeof message == 'string'))
 
{
   
var output = document.getElementById(elementId);
 
 
if (output.textContent !== undefined)
 
{
 output
.textContent = message;
 
}
 
else // using IE
 
{
 output
.innerText = message;
 
}
 
}
}


function process()
{
 
'use strict';
 
 
var dob;     // retrieve dob value from HTML
 
var msg;     // output error message
 
var dobDate; // convert dob to date type
 
  dob
= document.getElementById('dob');
  msg
= '';
 
 
if (dob && dob.value)
 
{
    dobDate
= new Date(dob.value);
   
if (dobDate.getTime())
 
{
 
}
 
else // invalid date or invalid input
 
{
  msg
= 'Invalid date!';
  setText
('errMsg', msg);
 
}
 
} // end if
}  // end process()


function init()
{
 
'use strict';
  document
.getElementById('frmReg').onsubmit = process;
}


window
.onload = init;

Web Code:
<!doctype html>
<html>
<head>
 
<meta charset="utf-8" />
 
<title>Registration Page</title>
 
<!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->

 
<link rel="stylesheet" href="css/mainstyle.css" />
 
</head>
 
<body>
   
<!-- registration page -->
   
<h1>Register Your Child</h1>
   
<p>This page allows you to enter your child's information in relation to the Two Hands preschool. </p>
   
<form action="" id="frmReg" novalidate>
     
<fieldset>
   
<legend class="fsheader">Identification</legend>
   
<p>Enter name and date of birth of the child that will be enrolling in the upcoming year. </p>
       
<div>
         
<label for="firstName">First Name: </label><input type="text" name="firstName" id="firstName" required />
       
</div>
   
       
<div>
     
<label for="lastName">Last Name: </label><input type="text" name="lastName" id="lastName" required />
       
</div>
   
<div>
     
<label for="dob">Birth Date: </label><input type="text" name="dob" id="dob" placeholder="mm/dd/yyyy" required />&nbsp;&nbsp;<span id="errMsg"></span>
   
</div>
     
</fieldset>
 
 
<fieldset>
   
<legend class="fsheader">Placement</legend>
   
<div>
     
<h2><label for="daysAttend">Days Attending: </label></h2>
 
<p>Which days will the child be attending preschool? </p>
 
<p>
   
<input type="checkbox" name="monday" id="monday" value="monday" />Monday <br />
   
<input type="checkbox" name="tuesday" id="tuesday" value="tuesday" />Tuesday <br />
   
<input type="checkbox" name="wednesday" id="wednesday" value="wednesday" />Wednesday <br />
   
<input type="checkbox" name="thursday" id="thursday" value="thursday" />Thursday <br />
   
<input type="checkbox" name="friday" id="friday" value="friday" />Friday <br />
 
</p>
   
</div>
   
<div>
     
<h2><label for="sched">Schedule Type: </label></h2>
 
<p>Which portion of the day will the child be attending? </p>
 
<select name="sched" id="sched">
   
<option value="Morning">Morning</option>
   
<option value="Afternoon">Afternoon</option>
   
<option value="FullDay">Both Morning and Afternoon</option>
 
</select>
   
</div>
   
   
<div>
     
<p />
     
<label for="register"></label><input type="submit" value="Register" id="submit" />
       
</div>
 
</fieldset>
   
</form>
   
<script src="js/reg.js"></script>
 
</body>
</html>

Thanks,

Patrick Wong
Newbie Web Developer

"On a clear disk, you can seek forever..."

--
You received this message because you are subscribed to the Google Groups "JavaScript Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javascript-information+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Screen refreshes after displaying error message

Saturday, September 20, 2014

0 comments
Hello,

I'm creating a registration page that, among other things, displays an error message when an invalid date is entered. The JavaScript I wrote detects the error and displays the error message -- for less than one (1) second -- then the entire page refreshes to display a blank registration page. Why does it do that and how can I get it to stop refreshing?

JavaScript Code:
function setText(elementId, message)
{
 
'use strict';
 
 
if ((typeof elementId == 'string') && (typeof message == 'string'))
 
{
   
var output = document.getElementById(elementId);
 
 
if (output.textContent !== undefined)
 
{
 output
.textContent = message;
 
}
 
else // using IE
 
{
 output
.innerText = message;
 
}
 
}
}


function process()
{
 
'use strict';
 
 
var dob;     // retrieve dob value from HTML
 
var msg;     // output error message
 
var dobDate; // convert dob to date type
 
  dob
= document.getElementById('dob');
  msg
= '';
 
 
if (dob && dob.value)
 
{
    dobDate
= new Date(dob.value);
   
if (dobDate.getTime())
 
{
 
}
 
else // invalid date or invalid input
 
{
  msg
= 'Invalid date!';
  setText
('errMsg', msg);
 
}
 
} // end if
}  // end process()


function init()
{
 
'use strict';
  document
.getElementById('frmReg').onsubmit = process;
}


window
.onload = init;

Web Code:
<!doctype html>
<html>
<head>
 
<meta charset="utf-8" />
 
<title>Registration Page</title>
 
<!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->

 
<link rel="stylesheet" href="css/mainstyle.css" />
 
</head>
 
<body>
   
<!-- registration page -->
   
<h1>Register Your Child</h1>
   
<p>This page allows you to enter your child's information in relation to the Two Hands preschool. </p>
   
<form action="" id="frmReg" novalidate>
     
<fieldset>
   
<legend class="fsheader">Identification</legend>
   
<p>Enter name and date of birth of the child that will be enrolling in the upcoming year. </p>
       
<div>
         
<label for="firstName">First Name: </label><input type="text" name="firstName" id="firstName" required />
       
</div>
   
       
<div>
     
<label for="lastName">Last Name: </label><input type="text" name="lastName" id="lastName" required />
       
</div>
   
<div>
     
<label for="dob">Birth Date: </label><input type="text" name="dob" id="dob" placeholder="mm/dd/yyyy" required />&nbsp;&nbsp;<span id="errMsg"></span>
   
</div>
     
</fieldset>
 
 
<fieldset>
   
<legend class="fsheader">Placement</legend>
   
<div>
     
<h2><label for="daysAttend">Days Attending: </label></h2>
 
<p>Which days will the child be attending preschool? </p>
 
<p>
   
<input type="checkbox" name="monday" id="monday" value="monday" />Monday <br />
   
<input type="checkbox" name="tuesday" id="tuesday" value="tuesday" />Tuesday <br />
   
<input type="checkbox" name="wednesday" id="wednesday" value="wednesday" />Wednesday <br />
   
<input type="checkbox" name="thursday" id="thursday" value="thursday" />Thursday <br />
   
<input type="checkbox" name="friday" id="friday" value="friday" />Friday <br />
 
</p>
   
</div>
   
<div>
     
<h2><label for="sched">Schedule Type: </label></h2>
 
<p>Which portion of the day will the child be attending? </p>
 
<select name="sched" id="sched">
   
<option value="Morning">Morning</option>
   
<option value="Afternoon">Afternoon</option>
   
<option value="FullDay">Both Morning and Afternoon</option>
 
</select>
   
</div>
   
   
<div>
     
<p />
     
<label for="register"></label><input type="submit" value="Register" id="submit" />
       
</div>
 
</fieldset>
   
</form>
   
<script src="js/reg.js"></script>
 
</body>
</html>

Thanks,

Patrick Wong
Newbie Web Developer
patrick@patrick.wong.name

"On a clear disk, you can seek forever..."

--
You received this message because you are subscribed to the Google Groups "JavaScript Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javascript-information+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

BEVERLY HILLS TOP PSYCHIC~ HONEST & AMUSING

Tuesday, September 16, 2014

0 comments
Your first 3 minutes are FREE talking live with me.

Please visit my website at: http://www.keen.com/Ask+Fran

Or, call me right now at: 1-800-275-5336 x0160

--
You received this message because you are subscribed to the Google Groups "Website Design Nz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to website-design-nz+unsubscribe@googlegroups.com.
To post to this group, send email to website-design-nz@googlegroups.com.
Visit this group at http://groups.google.com/group/website-design-nz.
For more options, visit https://groups.google.com/d/optout.

Copyright © 2010 Web Design | Free Blogger Templates by Splashy Templates | Layout by Atomic Website Templates

Vida de bombeiro Recipes Informatica Humor Jokes Mensagens Curiosity Saude Video Games Animals Diario das Mensagens Eletronica Rei Jesus News Noticias da TV Artesanato Esportes Noticias Atuais Games Pets Career Religion Recreation Business Education Autos Academics Style Television Programming Motosport Humor News The Games Home Downs World News Internet Design Entertaimment Celebrities 1001 Games Doctor Pets Net Downs World Enter Jesus Mensagensr Android Rub Letras Dialogue cosmetics Genexus lasofia thebushrajr wingshock tripedes gorduravegetal dainfamia dejavu-transpersonal jsbenfica republicadasbadanas ruiherbon iranianforaryans eaystcheyl fotosdanadir Só Humor Curiosity Gifs Medical Female American Health Madeira Designer PPS Divertidas Estate Travel Estate Writing Computer Matilde Ocultos Matilde futebolcomnoticias girassol lettheworldturn topdigitalnet Bem amado enjohnny produceideas foodasticos cronicasdoimaginario downloadsdegraca compactandoletras newcuriosidades blogdoarmario arrozinhoii