Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more

Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Web Design Forum > HTML / CSS
User Name
Password

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-23-2005, 03:38 PM   #1 (permalink)
mrasla
Junior Member
 
Join Date: Aug 2005
Posts: 27
Default Javascript field validation

I need to validate my Name field. Field should be only characters. No numbers in there. How do I validate (First name or Last Name fields)

thanks
mrasla is offline   Reply With Quote
Sponsored Links
Old 11-25-2005, 10:52 AM   #2 (permalink)
susanh
Junior Member
 
Join Date: Jun 2005
Posts: 29
Default

Hi,

a) include a maxlength
b) use a regex.
Code:
function validateCharsOnly(obj) { var re = /^[a-zA-Z]+$/; alert( re.test.(obj.value) ); }
Code:
<input type="text" name="t1" onblur="validateCharsOnly(this);" />
The test is for letters only.
susanh is offline   Reply With Quote
Old 11-25-2005, 06:50 PM   #3 (permalink)
vvaldir
Junior Member
 
Join Date: Jul 2005
Posts: 18
Default

Javascript, unlike many other languages, does not contain any functions to test fields for numeric or alphabetic. Instead it requires that you build these functions for yourself. Here is a set of functions to perform some of these tests for you.

If you want to be able to test if fields contain all numbers, all lowercase letters, all uppercase letters, letters (regardless of whether they are upper or lower case) or even letters and numbers (but without special characters) then copy the following Javascript and paste it into your page (or external Javascript code).

Code:
var numb = '0123456789'; var lwr = 'abcdefghijklmnopqrstuvwxyz'; var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; function isValid(parm,val) { if (parm == "") return false; for (i=0; i<parm.length; i++) { if (val.indexOf(parm.charAt(i),0) == -1) return false; } return true; } function isNum(parm) {return isValid(parm,numb);} function isLower(parm) {return isValid(parm,lwr);} function isUpper(parm) {return isValid(parm,upr);} function isAlpha(parm) {return isValid(parm,lwr+upr);} function isAlphanum(parm) {return isValid(parm,lwr+upr+numb);}

You can now test a field to see if it contains only numbers using isNum(field). To test a field for only letters use isAlpha(field), for only uppercase letters isUpper(field) or lowercase letters isLower(field). To test if it contains only letters and numbers without any other special characters use isAlphanum(field). In each case just substitute the name of the field to be tested for the word field and test the returned value - true indicates that the field contains at least one valid character and no invalid characters for the given test, false indicates that the field is either empty or contains at least one invalid character.

You can even set up your own special tests using the isValid function passing the valid values for the field in the second parameter. For example to test a field for numeric where decimals are allowed isValid(field,numb+'.'), to allow spaces as well as letters use isValid(field,lwr+upr+' '). You can even make these into standard functions by creating lines similar to the last five functions specified in the code.
vvaldir is offline   Reply With Quote
Old 11-27-2005, 06:58 AM   #4 (permalink)
magician_678
Member
 
Join Date: Nov 2005
Posts: 35
Default

Quote:
Originally Posted by susanh
Hi,

a) include a maxlength
b) use a regex.
Code:
function validateCharsOnly(obj) { var re = /^[a-zA-Z]+$/; alert( re.test.(obj.value) ); }
Code:
<input type="text" name="t1" onblur="validateCharsOnly(this);" />
The test is for letters only.

That line of code is :
Code:
alert( re.test(obj.value) );

btw thnx, i dint knew of regex in javascript, you got some tutorials or something for more?
magician_678 is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Points Per Thread View: 1.00
Points Per Thread: 11.00
Points Per Reply: 5.00



» Sponsors

» Links

» Affiliates
Web Hosting
Online Backup Reviews
Marketing Find
Merchant Select
SiteMap Builder
Host Compare
Dedicated Servers

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 11:13 PM.



LinkBacks Enabled by vBSEO 3.0.0 RC8
Webmaster Forums