Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-09-2005, 03:50 PM   PM User | #1
pml
New Coder

 
Join Date: Mar 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
pml is an unknown quantity at this point
JavaScript: Validation problems. Need to check for dots in form.

I have a form with three textboxes that I need to check that they don’t contain any dots (“.”). Thus the following combinations, for example, should return false (3434.3356) (.sdfsah.sdlfsdf) (pdhhpp.4235) All strings that contain a dot (or several) in any way should return false. How can I check for that? I have this code already and would appreciate if you could add to that, because I don’t know how to interpret and read regular expressions.


function check(){

if(document.form1.text1.value==""){ alert("You have a blank box"); return false; }
else if
// here I don’t know how to continue to check that there are no dots in the text box.


else
{ return true; }
}
pml is offline   Reply With Quote
Old 04-09-2005, 05:25 PM   PM User | #2
coothead
Senior Coder

 
coothead's Avatar
 
Join Date: Jan 2004
Location: chertsey, a small town 25 miles south west of london, england.
Posts: 1,545
Thanks: 0
Thanked 195 Times in 191 Posts
coothead will become famous soon enough
Hi there pml,

try this...
Code:
<script type="text/javascript">
<!--
function check() {

  var pattern=document.form1.text1.value;

if(pattern=="") { 
   alert("You have a blank box"); return false; 
 }
else {
if(pattern.search(/\./i )!=-1) { 
   alert("You have a dot in the box"); return false;
 }
else { 
   return true; 
   }
  }
 }
//-->
</script>
coothead
coothead is offline   Reply With Quote
Old 04-09-2005, 09:24 PM   PM User | #3
pml
New Coder

 
Join Date: Mar 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
pml is an unknown quantity at this point
Thumbs up Thanks!

Thank you for your very useful reply Coothead! It works perfectly fine now. However, even though I know what the reg.exp. does, I was just wondering if you coul explain how it functions. Basically I understand the code to the period in the middle but then the reg.exp. is closed with the "/", or am I wrong. Thereafter I don't know the function of "i" and the end I guess means not equal to -1. I just don't get all of it...and therefore it doesn't completely make sense. I would of course be happy if you could take a few seconds and explain it all to me...

pattern.search(/\./i )!=-1)

Thanks!

Pml
pml is offline   Reply With Quote
Old 04-10-2005, 07:54 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
!=-1 means "if the search returns a value not equal to -1", which in turns means that a . (period, dot) has been found at some position in the string (say at position 5 or whatever).

OK?

As a quibble,

if(pattern=="") {
alert("You have a blank box"); return false;
}

will return true if the box contains just a space character.
It might be prudent to have an additional test:-

if(pattern.search(/^\s+/i )!=-1) {
alert("You have a blank box"); return false;
}


i.e if the pattern to be matched starts with a space(s)
Philip M is offline   Reply With Quote
Old 04-11-2005, 05:58 AM   PM User | #5
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
The i there means case-insensitive, meaning the case (lower or upper) doesn't matter. But since the character to be matched is a dot or space, the i there is unnecessary.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:14 AM.


Advertisement
Log in to turn off these ads.