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:
  #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 14 m 9 sec
Reputation Power: 10
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: 6
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 14 m 9 sec
Reputation Power: 10
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: 6
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: 6
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




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek