Article Read. You Can Find All Kind of Articles

Home | Search Article

Search



Advanced Search

Validating Form Input in JavaScript

Kategori  Category : Web Development
Read  Times Read : 44
Date  Date : 26 March 2008 07:00

This time well make a form that collects information about thevisitor at your site. You must have filled-in copiousregistration forms or survey forms where you had to enter yourname, your email, your address, etc. Sometimes users,intentionally or unintentionally, enter wrong information thatcan either spoil your database scheme or give you lots of uselessdata and hence, waste your precious server space.To avoid such problems, as much as it can be managed, weprogrammatically try to make sure, that data is entered in anorderly fashion, and no unusable fields are entered. Checkingindividual fields of the form does this.Well see a form here with three fields: Name, Phone and Email.In this form, no field should be left blank, there should be nonumbers in the Name field [1,2,3,4,.], and in the Email field, noemail should be without the "@" sign. We can carry out morecomplex validations, but at the moment, these three shouldsuffice./// Remove the extra dots while testing. They have been justinserted so that some email programs dont freak out at thepresence of a JavaScript in the email.function CheckName(HoldName){ NoNumThere=true; for(i=0; i { for(j=0; j { if(HoldName.charAt(i)==j.toString()) { NoNumThere=false; break; } } if(NoNumThere==false) { break; } } return NoNumThere;}function CheckMail(HoldMail){ IsValid=true; if(HoldMail.indexOf("@") { IsValid=false; } return IsValid;}function checkfields(){ var AllFilled=true; for(i=0; i { if(visitor.elements[i].value.length==0) { alert("The field [" + visitor.elements[i].name + "] can not beleft blank."); AllFilled=false; visitor.elements[i].focus; return; } } if(AllFilled==true) { var NameValid=true; var EmailValid=true; NameValid=CheckName(visitor.vname.value); EmailValid=CheckMail(visitor.vemail.value); if(NameValid==false) { alert("Sorry, your name can not contain numbers."); visitor.vname.focus; } if(EmailValid==false) { alert("Sorry, this does not seem like a valid emailaddress."); } } if(NameValid==true & EmailValid==true) { alert("RIGHTO!!!"); }}Enter your name:Enter you phone:Enter your email:onclick="checkfields();"Copy and paste the code as it is, and save the entire content asa new HTML page. Then load it on to your browser. Unless you seethe result, itll be difficult to follow the script if you do nothave prior programming background. The first condition is, noneof the fields can be submitted blank. Click on the submit buttonwithout entering anything and observe the reaction.Here, we are making ample use of the recently learnt for(){.}loop. Then we have used function too, to carry out certainvalidations. Our main function, checkfields(), is associated withthe OnClick attribute of the "Submit" button, that is, when youclick on the "Submit" button, this function gets triggered.Some new terms in todays script are: true, false, charAt(),toString(), break, indexOf(), string.length, andorm.elements[ ].A quick explanation to make things easier:If at 10:30 pm, I say, "It is night", thenvar fact=trueand if I say at 10:30 pm that "Its afternoon", thenvar fact=falseWhich explains the use of true and false, which are also calledBoolean operators, which means, a Boolean variable can either betrue or false, but NEVER both.Until we learn about arrays, every character in a string has anindex position. For instance, if we havevar city="Delhi"then city.charAt(0)="D", city.charAt(1)="e",city.charAt(2)="l"...city.charAt(4)="i".toString(), converts another data type to a string data-type. Forexample,var num1=31var num2=21var char1=num1.toString()var char2=num2.toString()So,num1+num2=52andchar1+char2=3221In the second case, instead of being added, the variables arebeing concatenated, which indicates that they are strings, notnumbers. Well see its application later.break, true to its name, breaks something. In this case, itbreaks the loop in which it occurs, and takes the execution ofthe program to the line immediately after the loop, withoutmeeting the condition required to complete the loop.indexOf() tells us about the position of a particular characterin a string. Look its use in the following code:var city="Delhi"Referring to this code, city.indexOf("e") should give us a value1 and city.indexOf("h") should give us a value 3.city.indexOf("z") should give us a value less than zero,indicating that it does not belong to the given string.String.length gives us the length of the string, for instance, ifcity="Delhi", then city.length would give us 5.Again, elements[ ] is an array, and we havent dealt with themyet, so we leave the rest of the explanation to the next section.

About the Author

Amrit Hallan is a freelance web designer. For all web sitedevelopment and web promotion needs, you can get in touch withhim at amrit@bytesworth.com or http://www.bytesworth.com. Formore such articles,visit http://www.bytesworth.com/articles You can subscribe to hisnewsletter [BYTESWORTH REACHOUT] on Web Designing Tips & Tricksby sending a blank email at bytesworth-subscribe@topica.com

 

Web Development

Most Popular Articles

Random Article 1

Random Article 2

  • A Visit with FindWhat.com CEO, Craig Pisaris-Henderson
  • The Chairman and CEO of FindWhat.com (http://www.findwhat.com), Craig Pisaris-Henderson, recently attended a chat session and visited with Academy students regarding the FindWhat.com performance-based advertising network.This enlightening chat shed light on one of the leading pay-for-performance adv
  • 10 Things Every Business Web site Should Have
  • Business web sites are not just like any other sites. Companies make them to reach a lot of prospective customers around the world.Some times people forgot a lot of good things their business web sites should have. Today in this short article I list and explain ten things every business Web site sho

Random Article 3

Random Article 4

  • How to Write Effective Web Copy
  • Writing effective web copy begins with an understanding ofwhat the goals of your web site are. Are you trying to getyour visitors to purchase something or have them sign upfor your newsletter? Remember you are trying to get someoneyou cant see and have never met take a step towardsbuilding a relatio
  • HOW TO AVOID WEB SITE GROWING PAINS
  • According to a new survey carried out by Alliance & where ID_NUM=9270;Leicester, one in five small business owners view tax astheir greatest concern. The Chancellor has announced in hislast budget that companies with profits below œ10,000 willnot have to pay any corporation tax with effect from
http://kids-and-teens-blog.blogspot.com/ http://hobbies--blog.blogspot.com/ http://gadgets-and-gizmos-blog.blogspot.com/ http://education--blog.blogspot.com/ http://pets-and-animals-blog.blogspot.com/ http://legal-matters-blog.blogspot.com/ http://parenting-blog.blogspot.com/ http://site-promotion--blog.blogspot.com/ http://food-and-drink--blog.blogspot.com/ http://auto-and-trucks-blog.blogspot.com/ http://recreation-and-sports-blog.blogspot.com/ http://writing--blog.blogspot.com/ http://women--blog.blogspot.com/ http://home-improvement--blog.blogspot.com/ http://travel-and-leisure-blog.blogspot.com/ http://web-development--blog.blogspot.com/ http://family--blog.blogspot.com/ http://health--blog.blogspot.com/ indir teknolojix.com hiperucuz.com