|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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:
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))} //--> </script> |
|
#2
|
||||
|
||||
|
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] |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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>
|
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
I think I speak for everyone when I say: you're welcome!
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Validation help :( |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|