Thursday, August 2, 2007

My unforgetable past moments....



ASIM RAJ at Hans Vatika

Javascript getElementById

Javascript document.getElementById
If you want to quickly access the value of an HTML input give it an id and your life gets a lot easier. This small script below will check to see if there is any text in the text field "myText". The argument that getElementById requires is the id of the HTML element you wish to utilize.

function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

Things to Remember About getElementById

When using the getElementById function you need to remember a few things to ensure that everything goes smoothly. You always need to remember that getElementById is a method (or function) of the document object. This means you can only access it by: document.getElementById.

Also, be sure that you set your HTML elements' id attributes if you want to be able to use this function. Without an id you'll be dead in the water.

If you want to access the text within a non-input HTML element, then you are going to have to use the innerHTML property, instead of value. The next lesson goes into more detail about the uses of innerHTML.

JavaScript - Search

Advanced Search Function Examples

The following examples play around with the names a little so you can clearly see how the search function operates.

var myRegExp1 = /Tom|Jan|Alex/;
var string1 = "John went to the store and talked with Alexandra today.";
var matchPos1 = string1.search(myRegExp1);

if(matchPos1 != -1)
document.write("The first string found a match at " + matchPos1);
else
document.write("No match was found in the first string");

var myRegExp2 = /Tom|Jan|Alex /;
var string2 = "John went to the store and talked with Alexandra today.";
var matchPos2 = string2.search(myRegExp2);
if(matchPos2 != -1)
document.write("
The second string found a match at " + matchPos2);
else
document.write("
No match was found in the second string");

var myRegExp3 = /Tom|Jan|Alexandra/;
var string3 = "John went to the store and talked with Alexandra today.";
var matchPos3 = string3.search(myRegExp3);
if(matchPos3 != -1)
document.write("
The third string found a match at " + matchPos3);
else
document.write("
No match was found in the third string");

var myRegExp4 = /Tom|Jan|Alexandra/;
var string4 = "John went to the store and talked with Alex today.";
var matchPos4 = string4.search(myRegExp4);
if(matchPos4 != -1)
document.write("
The fourth string found a match at " + matchPos4);
else
document.write("
No match was found in the fourth string");


Display:

The first string found a match at 39
No match was found in the second string
The third string found a match at 39
No match was found in the fourth string

Javascript Form Validator | All Together Now

Below we have taken the HTML form code and the new function formValidator and plugged in all the other form validation functions taught in this lesson that are referenced in formValidator.

JavaScript/HTML Form validator...
The following code should be write in between //script tag
function formValidator()
{
// Make quick references to our fields
var firstname = document.getElementById('firstname');
var addr = document.getElementById('addr');
var zip = document.getElementById('zip');
var state = document.getElementById('state');
var username = document.getElementById('username');
var email = document.getElementById('email');
// Check each input in the order that it appears in the form!
if(isAlphabet(firstname, "Please enter only letters for your name"))
{
if(isAlphanumeric(addr, "Numbers and Letters Only for Address"))
{
if(isNumeric(zip, "Please enter a valid zip code"))
{
if(madeSelection(state, "Please Choose a State"))
{
if(lengthRestriction(username, 6, 8))
{
if(emailValidator(email, "Please enter a valid email address"))
{
return true;
}
}
}
}
}
}
return false;
}
function isEmpty(elem, helperMsg)
{
if(elem.value.length == 0)
{
alert(helperMsg);
elem.focus();
// set the focus to this input return true;
}
return false;
}
function isNumeric(elem, helperMsg)
{
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphabet(elem, helperMsg)
{
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphanumeric(elem, helperMsg)
{
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}
function lengthRestriction(elem, min, max)
{
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max)
{
return true;
}
else
{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}
function madeSelection(elem, helperMsg)
{
if(elem.value == "Please Choose")
{
alert(helperMsg);
elem.focus();
return false;
}
else
{
return true;
}
}
function emailValidator(elem, helperMsg)
{
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
return false;
}
}

gnustep-tutorial-html

GNUstep tutorial as HTML files
This is a collection of GNUstep tutorials in the HTML format covering: - Writing GNUstep Makefiles - Basic Classes of the GNUstep Base Library - First GUI Application: NSApplication, NSMenu - First GUI Application 2: NSWindow, NSButton - First GNUstep Renaissance Application - Distributed Objects - More on Makefiles: Libraries, Aggregate projects