How would you validate the contents of a "html" file using an external ".js" file?
I found this but I am new to javascript and do not know use functions like that. How do I send the request back to the html file once it has been processed?
Thank you,
Aaron
Last edited by Nightwalker83; Oct 2nd, 2007 at 12:22 AM.
Reason: Answered the question
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Should be easy work, but don't have the time now; I'll check back in in about 8 hours.
Thank you, so much!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Ok here we go. For your validation script safe this as an external javascript file. I haven't run any of this but it should be pretty close.
Code:
function ValidateInputs() {
var sName = test.full_name.value;
var sAddress = test.address.value;
var sSuburb = test.suburb.value;
var sPostCode = test.PostCode.value;
var sEmail = test.email.value;
var sCredit_card = test.credit_card.value;
var sMonth = test.month.value;
var sErr = "";
var sInstr = "";
//Check the fields
if (sName == "") {
sErr = "You must enter a full name\n";
}
if (sAddress == "") {
sErr = sErr + "You must enter an address\n";
}
if (sSuburb == "") {
sErr = sErr + "You must enter a Suburb\n";
}
if (sPostCode == "") {
sErr = sErr + "You must enter a Postal Code\n";
}
if (sEmail == "") {
sErr = sErr + "You must enter an email address\n";
}
if (sCredit_card == "Please choose your card!") {
sErr = sErr + "You must select a credit card type\n";
}
if (sMonth == "00") {
sErr = sErr + "You must select a month\n";
}
if (sErr == "") {
return true;
}
else {
sInstr = "The following errors were found with your submission."
sInstr = sInstr + "\nPlease correct them and try again.\n\n"
sErr = sInstr + sErr
//inform the user of the problem
alert(sErr);
return false;
}
}
This code just checks each of the fields and makes sure they don't have the default values. If it finds a default value it starts building an error message. After checking all the fields it check the error message and displays it if there is one. It then either cancels the form action if there was an error by returning false or signals everything is okay by returning true.
However, when I click the submit button regardless of whether there is info in the fields I get this (see attachment)!
Last edited by Nightwalker83; Oct 1st, 2007 at 08:13 PM.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
However, when I click the submit button regardless of whether there is info in the fields I get this (see attachment)!
That is because the action of your form was pointing the JS. The attached shows how it should have all been stringed together with the code I provided.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
function ValidateInputs() {
var sName = test.full_name.value;
var sAddress = test.address.value;
var sSuburb = test.suburb.value;
var sPostCode = test.post_code.value;
var sEmail = test.email.value;
var sCredit_card = test.credit_card.value;
var sMonth = test.month.value;
var sErr = "";
var sInstr = "";
//Check the fields
if (sName == "") {
sErr = "You must enter a full name\n";
}
if (sAddress == "") {
sErr = sErr + "You must enter an address\n";
}
if (sSuburb == "") {
sErr = sErr + "You must enter a Suburb\n";
}
if (sPostCode == "") {
sErr = sErr + "You must enter a Postal Code\n";
}
if (sEmail == "") {
sErr = sErr + "You must enter an email address\n";
}
if (sCredit_card == "please_choose") {
sErr = sErr + "You must select a credit card type\n";
}
if (sMonth == "00") {
sErr = sErr + "You must select a month\n";
}
if (sErr == "") {
return true;
}
else {
sInstr = "The following errors were found with your submission."
sInstr = sInstr + "\nPlease correct them and try again.\n\n"
sErr = sInstr + sErr
//inform the user of the problem
alert(sErr);
return false;
}
}
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
If you are using the html you posted then it wont run. You don't have a well formatted document. The document only contains a form. Your html doesn't contain the html, head and and body tags. It also doesn't link to the JS file. If you just run the page as is from the attachment it should work fine. Here is the complete html that you need. It contains all the elements required to work.
Yeah, I know it won't run will the form code I posted! I just posted the form code by its self to make it easier to figure out where the error was. I can't get this code to work in Fire Fox!
I don't use firefox much so I didn't know this. In the js I was referencing the form fields by FormName.FieldName.Value. This works fine in IE but for some reason FF wasn't detecting the form name as a valid object. To fix this I changed the js function to pass in a form and then used that to pull the form fields. The change to the js is
Code:
function ValidateInputs(objForm) {
var sName = objForm.full_name.value;
var sAddress = objForm.address.value;
var sSuburb = objForm.suburb.value;
var sPostCode = objForm.post_code.value;
var sEmail = objForm.email.value;
var sCredit_card = objForm.credit_card.value;
var sMonth = objForm.month.value;
var sErr = "";
var sInstr = "";
The html then needed to be changed so it was passing the form. The chages to the html are
Doesen't anbody use document.getElementById("myelementid") anymore. Guaranteed to work in most browsers.
var sName = document.getElementById("full_name").value;
Structure - Keep your html in html files.
Presentation - Keep you css in .css files.
Behaviour - Keep your javascript in .js files. Try to be unobtrusive.
Yeah, it's used for newer browsers.
As a professional web developer, you need to make your webpage compatible to new, but also old browsers.
You need to make it browser specific.
The thing is that there are more people using older Windows versions and browsers than you think.
type = "java script" but it made my form stop working!
Last edited by Nightwalker83; Oct 5th, 2007 at 01:34 AM.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
My point is that in a standards compliant world some of the methods used here are outdated in a similar way to using a font tag for example.
The aim is to use proper dom based methods for accessing elements and if these fail then if you wish to support very old browsers you can fall back on older methods.
Structure - Keep your html in html files.
Presentation - Keep you css in .css files.
Behaviour - Keep your javascript in .js files. Try to be unobtrusive.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672