mootools - simple field validation.
We’ve looked at some very basic AJAX functionality and some basic effects using mootools, but what about something as basic as form validations?
I took my previous mootools example and added a validate function:
function validate(form1){
if (form1) {
if ($(’yourname’).value.trim() == “”) {
alert(”Name field cannot be empty”);
$(’yourname’).focus();
return;
} else if ($(’yourmsg’).value.trim() == “”) {
alert(”Message field cannot be empty”);
$(’yourmsg’).focus();
return;
} else {
aStiiFunction();
}
} else {
if ($(’yourname1′).value.trim() == “”) {
alert(”Name field cannot be empty”);
$(’yourname1′).focus();
return;
} else if ($(’yourmsg1′).value.trim() == “”) {
alert(”Message field cannot be empty”);
$(’yourmsg1′).focus();
return;
} else {
aStiiFunction1();
}
}
}
This form takes a boolean (true or false) parameter to differentiate between whether it should validate the top form (true) or the bottom form (false). If the form1 parameter is set to true, it then checks whether the yourname field and the yourmsg field is not empty. If it is not, it will submit the form using the AJAX function aStiiFunction();
What makes it so easy is that you can simply check the two text input fields by calling the element ID of either field and compare it, for example: if ($(’yourmsg1′).value.trim() == “”)
Now remember, this is very basic and simple. You can do some pretty serious regular expression checking using the same validation. But that, my friends is a story for another day!
… and here to download the example.
technorati tags:mootools, ajax, javascript
Blogged with Flock
One Comment to "mootools - simple field validation."
Spit it out!
ajax, javascript, mootools Stii
Recent Posts
- Afrigator vs Regator
- Don’t pirate music/movies! You might be forced to use Windows if you do…
- Pike > Python?
- Using Twhirl for FriendFeed
- Being anti-social SUCKS!
- Example using XFN and Optimus to find friends on Afrigator
- Optimus, the ultimate Microformats parser
- Please support UnitedForAfrica.co.za
- Afrigator Beta2.0 - Lets amplify the Xenophobia issue
- What can we as bloggers do about the Xenophobia crisis?
My Posse
- Jayx’s bloggy
- Gogo’s blog
- Go2 South Africa
- Stumble Upon
- Dave Duarte
- Wikipedia
- zlythern
- Max Kaizen
- Tresblue
- Mike Stopforth
- RafiQ
- Muti.co.za
- Employmint
- Danette’s Bloggy!
- Thinking Machine
- White African
- kiefpiet.co.za
- Skuff’s World
- Goozeberry
- Crossloop blog
- Crossloop
- Aquila Online
- Charl van Niekerk
- Derek Allard
- Code Igniter
- Carls
- Justin Hartman
- blik.co.za
- Stefano Sessa
- Uno de Waal
- Amplitude!
- bLaugh
- Tyler Reed
- Chris Rawlinson
- Stormhoek!
- 3am
- Mike Solomon
- Mobile Q and A
- Eric Edelstein
- Marc Forrest
- Imel Rautenbach
- Absolutewillie
- Vincent Maher
- Colin Daniels
- Groogle!
- Chilibean
- Paul Jacobson
- Ayelet
- Python Guru Neil
- Rails Guru Nic
- Beverley Merriman
- Miguel
- Nic Harrywhatshisname
- Chris iMod
- Geekrebel!
- Steven McD
- Belinda sweetheart!
- Henre Rossouw
- JPGeek
- Foxinni
- Adii
- Charl Norman
- Bandwidthblog
- Jason Bagley
- Simon Botes
- Auric Silverwing
- Mark Forrester
- Saul Kropman
- Fred Roed
- Sass Schultz
Filed in
- Afrigator (26)
- ajax (9)
- API (2)
- Apple stuff (10)
- Blogging (24)
- browsers (5)
- Business (27)
- Code Igniter (8)
- firefox (8)
- flock (14)
- Funnies (72)
- GeekDinner! (18)
- General and sometimes Rants (47)
- Go2SA (2)
- ideas 2.0 (14)
- javascript (12)
- Kick-ass Tools (30)
- Linux (5)
- Marketing (25)
- moo.ajax (4)
- mootools (6)
- Open Source (10)
- Programming (33)
- C# (1)
- PHP (13)
- Python (9)
- Ruby (on Rails) (9)
- RSS (5)
- Semantic Web (32)
- Social Web (57)
- Software Development (15)
- South Africa (33)
- Tagging (6)
- Techie stuff (22)
- Tshirts (2)
- Tutorials (42)
- Blogging (17)
- Flocking (6)
- muti.co.za (13)
- Web 2.0 (73)
- web development (20)
Past Stuff
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006















Very easy. thanks..