Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 04-11-2008, 10:35 PM   #1 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default javascript: text validation


I have a form with 25 text boxes, named t1, t2, t3...t25. I expect the user to enter a single alphabet in capitals. When the user submits the button, I expect to check all the 25 elements for the following:
is it empty?
is it in capitals?
is the length of text restricted to 1?
is the input in numericals or symbols?

If found unacceptable, an alert displaying the error be made and focus sent to that field.

I can code to check each element one by one but that would result in repetition of same code 25 times, is there a way to do it in few lines?
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com
Sridhar_Rao is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 04-11-2008, 10:41 PM   #2 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: javascript: text validation

Put all textb oes into an array and loop through each value of that array!

And do post few lines of your JS code that are supposed to do the actual validation. We might suggest something better!

And I hope you are validating them on the server side too, because its easy to manipulate things on client-side. Your JS validation could fail!
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता

Last edited by victor_rambo; 04-11-2008 at 10:49 PM. Reason: Automerged Doublepost
victor_rambo is offline  
Old 04-11-2008, 11:06 PM   #3 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,676
Default Re: javascript: text validation

You may need to take a look at SPRY for unobtrusive and efficient validation.
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is offline  
Old 04-11-2008, 11:57 PM   #4 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: javascript: text validation

Using a reg-ex check should fit your need. Look into the code below. I have used ID instead of name.
Code:
function validateInput() {
	for (i = 1; i<= 5; i++) {
		var field = document.getElementById('t' + i);
		if(!/^[A-Z]$/.test(field.value)) {
			alert('Invalid Input!');
			field.focus();
			field.select();
			break;
		}
	}	
}
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 05-11-2008, 12:09 AM   #5 (permalink)
spice it up
 
kapsicum's Avatar
 
Join Date: Apr 2004
Location: mumbai
Posts: 106
Default Re: javascript: text validation

simply make a js function which contains all the validation u need i.e.
is it empty?
is it in capitals?
is the length of text restricted to 1?
is the input in numericals or symbols?

and while form submition call the function (in a loop) passing it two parameters viz. the fieldName (Not Field Value) & Alert box message(if u need to specify the name of unacceptable field)

*fieldName parameter is passed so the function can check its value using fieldName.value & also Focus on the field in question using fieldName.focus

ohh i just saw chandru.in's code.... its neat too
kapsicum is offline  
Old 06-11-2008, 07:57 AM   #6 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default Re: javascript: text validation

Thanks to the community, I got the solution. It is chandru's code that I was looking for. You may see its implementation at http://www.microrao.com/crossword1.htm

The funny thing about this javascript code is that, it works only in IE browsers and not in Firefox or Chrome. I tested it on all three and found to be working only in IE7.
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com
Sridhar_Rao is offline  
Old 06-11-2008, 11:16 AM   #7 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: javascript: text validation

The problem is that you have given only the "name" attribute for the text fields. For document.getElementById() to work, you must specify the "id" attribute. If you absolutely want to use only the name attribute, you can use document.getElementsByName().

The difference between "name" and "id" attributes is that value of "id" attribute should be unique in the page while "name" values can be repeated. In your case since you are anyway giving unique "name"s for all the text fields and using it for verification of solution, I'd suggest using "id" instead of "name". You could have used name if you could give same name for all text fields but in this case anyway it won't help you.

IE is notorious for killing web standards and acting in weird ways. In this case for some odd ball reason it returns the first component with given name when using getElementById(). Guess IE developers has gotta learn the difference between "name" and "id" and realize that there can be multiple elements with same name and returning only the first element with that name is brain-dead at best.
__________________
Chandru

http://tuxychandru.blogspot.com

Last edited by chandru.in; 06-11-2008 at 11:45 AM.
chandru.in is offline  
Old 06-11-2008, 06:09 PM   #8 (permalink)
In The Zone
 
Sridhar_Rao's Avatar
 
Join Date: Feb 2007
Posts: 353
Default Re: javascript: text validation

Thanks Chandru! So silly of me to have missed the point. I added ID to all the elements and now it is working perfectly. Even when I changed it to getElementByName, it still did not work, is this property supported only by IE?
__________________
Want to study M.Sc in any medical subjects? Read this www.microrao.com/msc.htm
Microx, a diagnostic microbiology laboratory software application www.labmicrox.com
Sridhar_Rao is offline  
Old 06-11-2008, 06:15 PM   #9 (permalink)
हॉर्न ओके प्लीज़
 
victor_rambo's Avatar
 
Join Date: Sep 2007
Posts: 1,493
Default Re: javascript: text validation

It should be getElementsByName().
It shall return an array containing all the elements with the given name.
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति

गीक होना माँगता
victor_rambo is offline  
Closed Thread

Bookmarks

Thread Tools
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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript text under mouse RandomNumber Programming 1 26-09-2008 10:10 AM
Changing label text using javascript whoopy_whale QnA (read only) 2 18-06-2007 10:20 AM
Unix text to notepad readable text? How? adithyagenius QnA (read only) 4 09-06-2007 07:27 PM
Image in text | bordered text goobimama Tutorials 5 03-02-2007 09:36 PM
Javascript form validation jugaaru QnA (read only) 3 07-12-2004 04:38 PM

 
Latest Threads
- by topgear
- by Charan

Advertisement




All times are GMT +5.5. The time now is 05:55 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2