Validating Form Input in JavaScript

Written by Amrit Hallan


Continued from page 1

Copy and pasterepparttar code as it is, and saverepparttar 119124 entire content as a new HTML page. Then load it on to your browser. Unless you seerepparttar 119125 result, it'll be difficult to followrepparttar 119126 script if you do not have prior programming background. The first condition is, none ofrepparttar 119127 fields can be submitted blank. Click onrepparttar 119128 submit button without entering anything and observerepparttar 119129 reaction.

Here, we are making ample use ofrepparttar 119130 recently learnt for(){.} loop. Then we have used function too, to carry out certain validations. Our main function, checkfields(), is associated withrepparttar 119131 OnClick attribute ofrepparttar 119132 "Submit" button, that is, when you click onrepparttar 119133 "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 explainsrepparttar 119134 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

Inrepparttar 119135 second case, instead of being added,repparttar 119136 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 breaksrepparttar 119137 loop in which it occurs, and takesrepparttar 119138 execution ofrepparttar 119139 program torepparttar 119140 line immediately afterrepparttar 119141 loop, without meetingrepparttar 119142 condition required to completerepparttar 119143 loop.

indexOf() tells us aboutrepparttar 119144 position of a particular character in a string. Look its use inrepparttar 119145 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 torepparttar 119146 given string.

String.length gives usrepparttar 119147 length ofrepparttar 119148 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 leaverepparttar 119149 rest ofrepparttar 119150 explanation torepparttar 119151 next section.

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


Using External JavaScript Files

Written by Amrit Hallan


Continued from page 1

I'm going to give you a small example.

Suppose your JavaScript file is "myjfuncs.js".

and it has a function to returnrepparttar bigger number ofrepparttar 105602 two supplied as parameters (you can simply start withrepparttar 105603 function, no need to write

So when we use one of its functions in a JavaScript:

In this way, we can use allrepparttar 105604 complex functions contained inrepparttar 105605 myjfuncs.js file.

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


    <Back to Page 1
 
ImproveHomeLife.com © 2005
Terms of Use