Validate Page on Submit Before Custom Javascript

by aherrick 12. August 2008 23:55

I was working on form submit where I wanted to prompt with a simple Javascript confirm dialog when the user clicks the submit button on the form.  Naturally, all the form fields (name, adress, etc.) I setup using required field validators.  My custom javascript confirm originally looked like this.

 

I called the javascript from the code behind.

The big problem I found was that the javascript confirm function was being called regardless of if the page completely validated or not.  I wanted the functionality to first validate the page, then if the page was valid, run the custom code.   I came up with this code using Page_IsValid.  It uses Page_ClientValidate() to validate the page, then I check if the page is valid.  Simple yet effective! 

Good luck!

Tags: , , ,

Read From XML Using Javascript

by aherrick 8. August 2008 07:08

Today I was working on some code to monitor my network speed (incoming and outgoing).  I wrote a windows service to capture my bandwidth and dynamically create an xml file and upload it to my website.  Every 30 seconds (time is totally configurable) I override the exisiting XML with an XML containing a fresh capture of my bandwidth speed.  How I wote the service is another post for another day...  However I do want to share some code I wrote to load the XML and update the text.

Here is an example of the XML file my windows service generates.

bandwidth xml

The XML file is named bandwidth.xml

I wrote a javascript function to attempt to read the XML file when it is in the same directory as my html file. See javascript function parseXML below.

bandwidth javascript

At the end of the function I make use of setTimeout to call the function again the function after a period of time.  In this case every 10 seconds.

I load the Javascript function using the body onload event.

body onload="parseXML()"

I have three span tags in my html.  XML, upload, and download.  I grab their IDs in the function and use innerHTML to replace the text.  Try it out by loading the html in a browser and changing the xml values.  Have some fun with it!

Check out the XML and HTML below to see the full code.  Good luck!


bandwidth.xml (100.00 bytes)

index.html (1.43 kb)

Tags: , ,

javascript validate textbox onblur

by aherrick 9. July 2008 05:12

Today I was working on some code and I needed to validate a textbox input when the user attempted to leave the field.  The javascript event onblur fit the bill nicely. 

In the code behind I wired up my textbox like so:

this.txtAmount.attributes.add("onblur", "validate()");

This will hook the onblur event onto the textbox programatically.  I find it appropriate to add events progrmatically since Visual Studio complains about adding events to the markup.  While this will still compile and run just fine, there is much more flexiblity in adding events in the code behind.

Now for the javascript code.  First I am setting a reference to my textbox and a hidden field on the form.  The first check is a regular express to make sure my input is valid.  I am looking for positive amount.

For example: 2 valid, 200 valid, 200.20 valid, 200.233 invalid, -200 invalid. 

If input does not pass the regex, I throw an alert asking for valid input.  If it passes regex, my second check is just some business logic.

That is all it takes to write a custom javascript validation for your code. Thank you!

Tags: , ,

create javascript pop up window link using c#

by aherrick 8. July 2008 07:50

Today I was working on some code and I needed to programatically create a link with the ability to have a javascript pop up.  The code is as follows:

string javascriptLink = "<a href=\"javascript: void(0)\" onclick =\"window.open('YourPageHere.aspx', '', 'scrollbars=0');return false;\">javascript link</a>";

To take this code a little further as in my case I needed to pass a query string to the page I wanted to load.

Edit your string like so:

string javascriptLink = "<a href=\"javascript: void(0)\" onclick =\"window.open('YourPageHere.aspx?Id=" + yourId + "', '', 'scrollbars=0');return false;\">javascript link</a>";

where yourId is the variable you want to pass.

Let me know if it worked for you. It's just that easy! 

Tags: ,



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen
Tweaked by ajondeck.NET

About the author

Andrew Herrick
I am a full time developer for Blue Horseshoe Solutions based out of Carmel, Indiana. I enjoy learning new technology and hope to give back with some of the cool things I pick up along the way.  Check me out on Stack Overflow!


Categories

None

Recent comments

Comment RSS

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008