|
|
|
Dating and Doors
Has it been a while since you have been out on a date, or maybe you haven't been able to get a second date after your first date fiasco, this article is written as a refresher course into dating etiquette and for your learning more about what your...
Dating With Confidence
Dating can be a nerve-wracking experience for many people. After
all, you could be meeting your future spouse. It can also be a
very vulnerable experience. The whole point of dating (usually)
is to get to know someone else on an intimate level,...
Getting More From Dating, Romantic Relationships, and Marriage
Romantic relationships happen because of the hopes and dreams a couple has for a happy life together. The relationship will continue to be pleasant and rewarding as long as the hopes and dreams are kept alive by staying concerned with what is...
Online Dating Works!
You hear it everyday. There are more and more people out there who have found love online. I am not surprised! From my experience, online dating is so much easier and better then offline dating. Just a few of years ago, I was single and very...
Thai Girls Dating : A Thai Girl is Giving Her Opinion !
Thai Girls dating is becoming very popular !
1. Western guys don't have access to the same type of woman that Thai man have.
This was I think quite true until 10 years ago. But especially with advent of internet many decent girls (I don't like...
|
|
| |
|
|
|
|
|
|
Validating Form Input in JavaScript
This time we'll make a form that collects information about the visitor at your site. You must have filled-in copious registration forms or survey forms where you had to enter your name, your email, your address, etc. Sometimes users, intentionally or unintentionally, enter wrong information that can either spoil your database scheme or give you lots of useless data and hence, waste your precious server space.
To avoid such problems, as much as it can be managed, we programmatically try to make sure, that data is entered in an orderly fashion, and no unusable fields are entered. Checking individual fields of the form does this.
We'll see a form here with three fields: Name, Phone and Email. In this form, no field should be left blank, there should be no numbers in the Name field [1,2,3,4,.], and in the Email field, no email should be without the "@" sign. We can carry out more complex validations, but at the moment, these three should suffice.
/// Remove the extra dots while testing. They have been just inserted so that some email programs don't freak out at the presence of a JavaScript in the email.
<..script language="JavaScript1.2"> function CheckName(HoldName) { NoNumThere='true'; for(i=0; i { for(j=0; j<10; j++) { if(HoldName.charAt(i)==j.toString()) { NoNumThere='false'; break; } } if(NoNumThere=='false') { break; } } return NoNumThere; } function CheckMail(HoldMail) { IsValid='true'; if(HoldMail.indexOf("@")<=0) { IsValid='false'; } return IsValid; } function checkfields() { var AllFilled='true'; for(i=0; i<3; i++) { if(visitor.elements[i].value.length==0) { alert("The field [" + visitor.elements[i].name + "] can not be left 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 email address."); } } if(NameValid=='true' & EmailValid=='true') { alert("RIGHTO!!!"); } }
Copy and paste the code as it is, and save the entire content as a new HTML page. Then load it on to your browser. Unless you see the result, it'll be difficult to follow
the script if you do not have prior programming background. The first condition is, none of the fields can be submitted blank. Click on the submit button without 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 certain validations. Our main function, checkfields(), is associated with the OnClick attribute of the "Submit" button, that is, when you click on the "Submit" button, this function gets triggered.
Some new terms in today's script are: true, false, charAt(), toString(), break, indexOf(), string.length, and orm.elements[ ].
A quick explanation to make things easier:
If at 10:30 pm, I say, "It is night", then
var fact='true'
and if I say at 10:30 pm that "It's afternoon", then
var fact='false'
Which explains the use of true and false, which are also called Boolean operators, which means, a Boolean variable can either be true or false, but NEVER both.
Until we learn about arrays, every character in a string has an index position. For instance, if we have
var 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. For example,
var num1=31 var num2=21 var char1=num1.toString() var char2=num2.toString()
So,
num1+num2=52 and char1+char2=3221
In the second case, instead of being added, the variables are being concatenated, which indicates that they are strings, not numbers. We'll see its application later.
break, true to its name, breaks something. In this case, it breaks the loop in which it occurs, and takes the execution of the program to the line immediately after the loop, without meeting the condition required to complete the loop.
indexOf() tells us about the position of a particular character in a string. Look its use in the following code:
var city="Delhi"
Referring to this code, city.indexOf("e") should give us a value 1 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, if city="Delhi", then city.length would give us 5.
Again, elements[ ] is an array, and we haven't dealt with them yet, 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 site development and web promotion needs, you can get in touch with him at amrit@bytesworth.com or http://www.bytesworth.com. For more such articles, visit http://www.bytesworth.com/articles You can subscribe to his newsletter [BYTESWORTH REACHOUT] on Web Designing Tips & Tricks by sending a blank email at bytesworth-subscribe@topica.com
|
|
|
|
|
|