I am fairly new to web development but am working as an intern this semester and learning everything I can.
A project I was given was to take a form, from a template, and use that to create a new form for a different purpose. The "template" I was given used a C# script that would validate the email address and then send the form data as a text email. To say the least, the form I was given to work with was inadequate and not very well done. The template had no client side validation so none of the forms were required to be submitted and if an invalid email address was given, the page failed displaying an error that is of no use to anyone without access to the server.
So, I created the new form which just required adding new field elements. However, I wanted to add client-side validation. I wrote scripts in Javascript to validate the email address and verify that valid fields were filled out. The form uses ASP form fields in order to submit the data to the server. Because of this, I could not add the Javascript (I'm told) as would normally be done in HTML because asp fields don't have attributes such as onblur, onfocus, etc. To handle this, I created added a Page_Load function to the C# scripts that would add the appropriate attributes to the appropriate fields.
void Page_Load(object sender, EventArgs e) {
if(instance==null){
Submit.Attributes.Add("onfocus", "completeAddress('quoteForm', 'LocAddSame','onsubmit'); validateForm('quoteForm'); blurButton('Submit'); ");
LocAddSame.Attributes.Add("onclick", "completeAddress('quoteForm','LocAddSame', 'inForm')");
Email.Attributes.Add("onblur", "validateField('emailAddress', 'Email')");
Other.Attributes.Add("onclick", "enableTextbox('quoteForm','Other','otherProfCenter')");
instance = "instantiated";
}
}
This appears to work well for most of the scripts. I have a script to auto-complete the address field if the "Same As" checkbox is check. I have a script to display a textbox if the "other" checkbox is checked. Those scripts run fine.
(Actually, the whole page works correctly in IE and Firefox).
However, using a more compliant browser such as Chrome, Safari, or Opera, the validation script does not run. The validation js is to check to see if the required fields are validated. If the fields are valid, true is returned. If they are not valid, false is returned, the style sheet of the field objects are changed to a pink to "highlight" them, and an window pops up to say that required fields are not valid. The returning false is to keep the page from submitting, which occurs onclick, but after the Javascript passes.
The submit button is as follows in the code...
<asp:Button id="Submit" Text="Submit" runat="server" onclick="SendEmail"/>
When the webpage renders it renders as...
<input type="submit" name="Submit" value="Submit" id="Submit" onfocus="completeAddress('quoteForm', 'LocAddSame','onsubmit'); validateForm('quoteForm'); blurButton('Submit'); " />
Again, this works in IE and Firefox, but in the other browsers, clicking the "Submit" button submits the form, or attempts to, whether the fields are valid or not.
What I'm hoping for are some suggestions as to how I can prevent the field from submitting before it is validated. I believe if I had to do over, I would use much different methods than I did here to validate and submit the form. However, it is what it is and I had to work with what my employer gave me. They are ok with it working only on IE and FF, but I would really like to get it working on all browsers. I'm looking for advice on changes I could try to fix my problem without a complete overhall. I don't mind putting some work, thought, and time into this yet, but I just can't start over. Any suggestions would be greatly appreciated. I can provide more information as needed to help you all understand what I've done so far.

New Topic/Question
Reply



MultiQuote



|