JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old March 9th, 2005, 11:39 AM
jimmyd4ng3r jimmyd4ng3r is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 2 jimmyd4ng3r User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 52 sec
Reputation Power: 0
Red face Validation help :(

OK, a bit new to the javascript seen but know it has a lot to offer. At the moment I'm just writing a web site to connect to an SQL database and so users can enter data through the website and into the database....ANY HOW, i'm trying to use java script to validate input for a Field called "DNAME" in a form called "FORM1". So far I have only been able to do validation for the input to be between 1 & 14 char's (poor i know). What I really want is for:
  1. the "-" and " " chars must be entered (" " = space)
  2. the "-" and " " char must not be at the start or last char entered
  3. "-" can not be the immediate neighbour of " "
  4. "-" or " " must not be adjacent to itself
If any one can help me on any of them, i'd b very grateful !!!

P.S. jus in case u wana see what i done so far, here it is:

<script language="JavaScript">
<!--

function check()
{
send = 'yes'
if ((document.form1.dname.value.length <1) || (document.form1.dname.value.length > 14))
{
alert("Department Name must be between 1 & 14 characters")
send='no'
}
}

//-->
</script>

Reply With Quote
  #2  
Old March 9th, 2005, 12:53 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
smells like homework...

Have you looked into Regular Expressions?
Although if you're new to javascript, chances are Regex will be over your head.

You might want to look into charAt() method.
Or the many other Javascript String Functions

Your first two points aren't that difficult [refer to charAt()]...
The other two points require minimal thinking, but again use charAt() or search() [for example]

Reply With Quote
  #3  
Old March 9th, 2005, 08:02 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
I think RegExp would make it really simple and short!


^([a-zA-Z0-9]+-[a-zA-Z0-9]+\s[a-zA-Z0-9]+)+$

That's the regexp to use (not perfect..but I think it will do). if no numbers are to be entered, remove the 0-9 part everywhere!!
Are you allowed to enter more than just one - and one " " ? If not, use:

^[a-zA-Z0-9]+-[a-zA-Z0-9]+\s[a-zA-Z0-9]+$

Since I know you're going to ask...the code would be:

Code:
   
var re = new RegExp(/....<strange thing I gave you>...../)
if(document.theForm.theField.value.match(re))
	do code for the match
else
	do code for no match 


Please MadCowDzz...correct me if I'm mistaken (you have more knowlage on Javascript than I do)

Good Luck to all

ANibal.
Comments on this post
MadCowDzz agrees: Nice regular expression

Reply With Quote
  #4  
Old March 10th, 2005, 07:49 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
Fancy stuff Anibal... I love regular expressions, I was just too scared/lazy to write it =)
The only thing your regex doesn't take into account is the 14 character length, which is fine because jimmyd4ng3r already figured that part out.
I tested it against "11-1 11" and it validated...

If anyone's interested, here's the full script I used to test:
Code:
<html>
<head>
<script>
function checkMatch(str) {
	// Regular Expression thanks to Anibal of Devarticles forums
	var re = new RegExp(/^([a-zA-Z0-9]+-[a-zA-Z0-9]+\s[a-zA-Z0-9]+)+$/) 
	if(str.length<14 && str.match(re))
		return true;
	else
		return false
}
</script>
</head>

<body>
<input type="text" id="myBox" />
<input type="button" value="Check" onclick="alert('Match?\n'+checkMatch(document.getElementByI  d('myBox').value));" />
</body>
</html>

Reply With Quote
  #5  
Old March 10th, 2005, 08:04 AM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
Thanks MadCowDzz! Yes, I love RegExp too (altough I wouldn't call this one fancy...too much credit for me!) I think they save a lot of time and code.
I didn't see the 14 character limit. Better!, that way...he gets use to working with regexp and also plain Javascript (I sound like a school teacher!! ).

Good Luck to you all

Anibal.

Reply With Quote
  #6  
Old March 11th, 2005, 05:45 PM
jimmyd4ng3r jimmyd4ng3r is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 2 jimmyd4ng3r User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 52 sec
Reputation Power: 0
Talking

Cheers peeps, i very grateful. I wish i could grasp programming languages as well as you lot. I a networking student and for some reason I have to make web sites on my course. I also have to do Java Networking, and thats HARD. No doubt you lot would find that module a pleasure...wish i did


once again, MUCH appriciated

jimi

Reply With Quote
  #7  
Old March 14th, 2005, 10:19 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
I think I speak for everyone when I say: you're welcome!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Validation help :(


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway