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

tap into onsubmit event?

I have a function that validates a text box. Is there anyway to run that
function on submit and if it returns false to cancel the submit. Any ideas.
Thanks Guys! This group is awesome.

Thanks,
Chris
Oct 20 '05 #1
8 1772

Chris Kettenbach wrote:
I have a function that validates a text box. Is there anyway to run that
function on submit and if it returns false to cancel the submit. Any ideas.
Thanks Guys! This group is awesome.

Thanks,
Chris


here's a small sample:

<form id = "myForm" onsubmit = "return validateForm()">
<input type = "text" name = "textBox"/>
<input type = "submit"/>
</form>

And then in your javascript:

function validateForm()
{
var myForm = document.forms["myForm"];
var error = true;

if(myForm.elements["textBox"].value != "")
{
error = false;
}
else
{
error = true;
}

return (err) ? false : true;
}

Oct 20 '05 #2
The only problem is that this is an aspx page and i am working in a
template. I don't really have a way to add markup to the form element. Any
other suggestions?
Chris

"web.dev" <we********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

Chris Kettenbach wrote:
I have a function that validates a text box. Is there anyway to run that
function on submit and if it returns false to cancel the submit. Any
ideas.
Thanks Guys! This group is awesome.

Thanks,
Chris


here's a small sample:

<form id = "myForm" onsubmit = "return validateForm()">
<input type = "text" name = "textBox"/>
<input type = "submit"/>
</form>

And then in your javascript:

function validateForm()
{
var myForm = document.forms["myForm"];
var error = true;

if(myForm.elements["textBox"].value != "")
{
error = false;
}
else
{
error = true;
}

return (err) ? false : true;
}

Oct 20 '05 #3
web.dev said the following on 10/20/2005 2:28 PM:
Chris Kettenbach wrote:
I have a function that validates a text box. Is there anyway to run that
function on submit and if it returns false to cancel the submit. Any ideas.
Thanks Guys! This group is awesome.

Thanks,
Chris

here's a small sample:

<form id = "myForm" onsubmit = "return validateForm()">
<input type = "text" name = "textBox"/>
<input type = "submit"/>
</form>

And then in your javascript:

function validateForm()
{
var myForm = document.forms["myForm"];
var error = true;

if(myForm.elements["textBox"].value != "")
{
error = false;
}
else
{
error = true;
}

return (err) ? false : true;


Why make it more difficult than it needs be?

return error;

As 'err' is undefined.

The else statement is not needed either. It only sets error to true and
it was true to begin with. Although it may be better reasoning to set
error=false and then return !error

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 20 '05 #4
> The only problem is that this is an aspx page and i am working in a
template. I don't really have a way to add markup to the form element. Any
other suggestions?


function addValidation(){
document.forms["formName"].onsubmit = function(){ return
validateForm(); }
}
window.onload = addValidation;

Oct 20 '05 #5
Lee
Jambalaya said:
The only problem is that this is an aspx page and i am working in a
template. I don't really have a way to add markup to the form element. Any
other suggestions?


function addValidation(){
document.forms["formName"].onsubmit = function(){ return
validateForm(); }
}


document.formName.onsubmit=validateForm;

Oct 20 '05 #6
web.dev wrote:
[snip]
And then in your javascript:

function validateForm()
{
var myForm = document.forms["myForm"];
var error = true;

if(myForm.elements["textBox"].value != "")
{
error = false;
}
else
{
error = true;
}

return (err) ? false : true;
}

Why not:

function fieldNotEmpty(fieldName){
return fieldName.value != "";
}

?
Mick
Oct 21 '05 #7
Mick White wrote:

Why not:

function fieldNotEmpty(fieldName){
return fieldName.value != "";
}

?
Mick


Oops, poor choice of parameter name:
function fieldNotEmpty(field){
return field.value != "";
}
Mick
Oct 21 '05 #8
JRS: In article <DJ********************@comcast.com>, dated Thu, 20 Oct
2005 16:16:13, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
web.dev said the following on 10/20/2005 2:28 PM:

And then in your javascript:

function validateForm()
{
var myForm = document.forms["myForm"];
var error = true;

if(myForm.elements["textBox"].value != "")
{
error = false;
}
else
{
error = true;
}

return (err) ? false : true;


Why make it more difficult than it needs be?

return error;

As 'err' is undefined.

The else statement is not needed either. It only sets error to true and
it was true to begin with. Although it may be better reasoning to set
error=false and then return !error

function validateForm() {
return document.forms["myForm"].elements["textBox"].value != "" }

or possibly == "" or > "" or

function validateForm() {
return !!document.forms["myForm"].elements["textBox"].value }
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
The Big-8 newsgroup management is attempting to legitimise its questionable
practices while retaining its elitist hegemony. Read <URL:news:news.groups>.
Oct 21 '05 #9

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

Similar topics

4
by: Stuart Wexler | last post by:
Hi, I have a form with onSubmit embedded in the <form> tag. The form is submitted programatically through javascript . While the form submits fine, nothing I'm doing seems to get it to...
2
by: Sean Dockery | last post by:
Which is the following is correct? a) <form ... onSubmit="return checkData()"> b) <form ... onSubmit="return checkData();"> c) <form ... onSubmit="checkData()"> d) <form ......
4
by: usl2222 | last post by:
Hi folks, I appreciate any assistance in the following problem: I have a form with a bunch of dynamic controls on it. All the controls are dynamically generated on a server, including all...
4
by: Tina | last post by:
I have some javascript that I want executed whenever my form is subitted so I put onSubmit= "return myfunction();" in my <form clause on my aspx page. If the user hits a button the javascript...
2
by: Craig | last post by:
This has got to be easy, but I can't figure it out. I have a login control with a login button to process the username,password, etc. I have the login control within a login page, and this page...
2
by: Sorrow | last post by:
If you have an onsubmit event handler defined for a form, it doesn't seem to trigger if you manually call the .submit() method of the form object. Is there any way you can make sure that code is...
8
by: Mark Livingstone | last post by:
I have a form that uses the following: onSubmit="some_var = 'validated';" FireFox is OK with that. Internet Explorer isn't. any ideas why? Thanks.
1
by: vunet | last post by:
I write a JS library component which is applied to every form on a webpage. The component does something before it submits the form. Now, let's say user has his own onSubmit() handlers returning...
1
by: Phyrefly | last post by:
Hi, I have a form, which is sometimes submitted by clicking a submit button, sometimes by enter being pressed in a field, and sometimes programmatically (By the clicking of a "Master Save"...
2
by: rudiedirkx | last post by:
Gents, I have a problem (only in Safari) with the onsubmit in webforms. This topic covers the same subject: http://bytes.com/topic/javascript/answers/166542-onsubmit-safari but not as detailed as...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.