AllExperts > Experts 
Search      

Javascript

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Javascript Answers
Question Library

Ask a question about Javascript
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Anthony Levensalor
Experience
I have been programming in Java and Javascript since 1998. I run a web development company that specializes in Ajax front ends with PHP/MySQL back ends. I am a Sun Certified Java 2 Developer, and have done nothing but web applications for the last two years in my business

Past/Present clients
Monster.com, Compuware Corporation, Flextronics International, Pragmatech Software, Open Travel Software, The Loss Mitigation Institute, Raw Story Media, Page One News Media.

 
   

You are here:  Experts > Computing/Technology > Focus on JavaScript > Javascript > mandatory field

Topic: Javascript



Expert: Anthony Levensalor
Date: 10/19/2007
Subject: mandatory field

Question
hie there!

i need an answer from u guys. I have a free form which part of the question is a mandatory field. I use submit button to pass a value to save new input to database. Before that, i have a checking where system not save data untill all mandatory fields are inserted. The problem is, when there's unfill field for mandatory it goes back to previous page. I want maintain the previous data, so that user dont have to fill in all the questions. I used history.go(-1), but i can't retain the previous data (which is i not save yet!)

Answer
Put your checking in the same page as the form, and use onsubmit in the form tag. That way, you can cancel the submission instead of going ahead and losing your data.

<form name="frmName" action="url" method="POST" onsbumit="return checkForm();">
<input type="text" name="first_name"/>
</form>

Then, in the script:
<script type="text/javascript">
function checkForm()
{
  var frm = document.forms['frmName'];

  var ok = true;
  var msg = "";
  
  if (frm['first_name'].value.length==0)
  {
    msg += "First Name may not be blank.";
    ok = false;
  } // if

  if (!ok)
  {
    alert(msg);
  }

  return ok;
} // checkForm
</script>

This will stop them on the blank field, with no need to navigate.

Hope this helps,
~A!  

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.