PDA

View Full Version : New to JavaScript



malickscript
10-31-2008, 01:28 PM
Hi,
I'm New to java Script ,i need to validate some text field,email field,number filed ,give me some advice to do that,any links to refer any ebooks.

Thanks in advance
Malick

Twey
10-31-2008, 01:44 PM
Javascript is not the tool for the job in this case. Validation must be performed server-side (although you may wish to do it on the client as well, for convenience).

rangana
11-01-2008, 02:41 AM
I do agree with Twey, but as per request, you can validate a textbox's value if it's a valid email by RegEx (http://www.javascriptkit.com/jsref/regexp.shtml):



<script type="text/javascript">
var ray={
checkEmail:function(el)
{
alert(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(this.returnVal(el))?'Congratulations! It\'s a valid email address':'Sorry, it\'s an invalid email address');
},
checkNum:function(el)
{
// For regex, you can use \d - to test it value is decimal
alert(!isNaN(Number(this.returnVal(el)))?'It\'s a valid number':'It\'s an invalid number');
},
returnVal:function(el)
{
return document.getElementById(el).value;
}
}
</script>
<label for="email">Email Address: </label>
<input type="text" id="email"><br>
<label for="number">Number: </label>
<input type="text" id="number"><br>
<input type="button" value="Validate Email" onclick="ray.checkEmail('email')">
<input type="button" value="Validate Number" onclick="ray.checkNum('number')">