...

View Full Version : JavaScript: Validation problems. Need to check for dots in form.



pml
04-09-2005, 04:50 PM
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; }
}

coothead
04-09-2005, 06:25 PM
Hi there pml,

try this...

<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

pml
04-09-2005, 10:24 PM
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

Philip M
04-10-2005, 08:54 AM
!=-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)

glenngv
04-11-2005, 06:58 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum