Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

onsubmit validation with js not working in firefox

Posted on 2005-04-24
4
Medium Priority
?
1,138 Views
Last Modified: 2012-05-05
im trying to use javascript to validate a form using onsubmit, but firefox is ignoring the javascript and not blocking the submission (it works fine in ie6), the code is below:



function checkscript(form1) {

if(document.forms.form1.first_name.value=="")
{
alert("Please enter your first name.");
return false;
}

if(document.forms.form1.last_name.value=="")
{
alert("Please enter your last name.");
return false;
}

if(document.forms.form1.username.value=="")
{
alert("Please enter your username.");
return false;
}

return true;

}

<form name="form1" id="form1" method="post" onsubmit="return checkscript()" action="process.php">

0
Comment
Question by:wrynn
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Xxavier
ID: 13855629
Hi wrynn!! Its Xxavier here!! :0)  

Probably should be

document.form1.first_name.value==""

not

document.forms.form1.first_name.value==""

Ta Da!!

Xxavier :0)
0
 
LVL 7

Accepted Solution

by:
Xxavier earned 1600 total points
ID: 13855662
Hi wrynn!!, Its Xx again!! this works fine cross browser :0)

<script>
function checkscript() {
if(document.form1.first_name.value=="")
{
alert("Please enter your first name.");
return false;
}
if(document.form1.last_name.value=="")
{
alert("Please enter your last name.");
return false;
}

if(document.form1.username.value=="")
{
alert("Please enter your username.");
return false;
}

return true;

}
</script>

<form name="form1" id="form1" method="post" onsubmit="return checkscript()" action="http://google.com/search">
  <p><input name="first_name" size="20"> </p>
  <p><input name="last_name" size="20"> </p>
  <p><input name="username" size="20"> </p>
  <p><input type="submit"></p>
</form>
0
 
LVL 30

Assisted Solution

by:third
third earned 400 total points
ID: 13855708
for a much easier form elements handling, try passing the form as a parameter to the function. like,

function checkscript(obj) {

  if(obj.first_name.value==""){
    alert("Please enter your first name.");
    return false;
  }

  if(obj.last_name.value==""){
    alert("Please enter your last name.");
    return false;
  }

  if(obj.username.value==""){
    alert("Please enter your username.");
    return false;
  }

  return true;
}

<form name="form1" id="form1" method="post" onsubmit="return checkscript(this);" action="process.php">


it makes your code shorter, neatier and more readable.
0
 

Author Comment

by:wrynn
ID: 13855866
thanks guys.  didnt realize i was passing the form object and not even using it!  also i deleted the extra "form" before .form1.field.value

i guess maybe i was using the wrong <script> tag too
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question