SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: form check
-
Apr 10, 2002, 09:40 #1
- Join Date
- Sep 2001
- Posts
- 94
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
form check
I want to do a check where a field has to be an integer and the length has to be 4 or 6. Can someone help?
I'm trying to do the length thing but it is not working and I have no idea how to do the integer check.
if (document.comment.cust.value.length != 6 || 4)
{
alert("Please enter a valid Cust #")
document.comment.cust.focus()
return false
}
I can do this so if less than 4, but I really don't want 5,7,8,9... to go through:
if (document.comment.cust.value.length < 4)
{
alert("Please enter a valid Cust #")
document.comment.cust.focus()
return false
}
Thanks.Last edited by jamesb; Apr 10, 2002 at 09:51.
-
Apr 10, 2002, 10:29 #2
- Join Date
- Nov 2001
- Location
- Maryland
- Posts
- 175
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:if (document.comment.cust.value.length > 6 || document.comment.cust.value.length < 4 || document.comment.cust.value.length == 5) { alert("Please enter a valid Cust #") document.comment.cust.focus() return false }
Drew~Drew
There Is No Greater Joy Than Soaring High On The Wings Of Your Dreams, Except Maybe The Joy Of Watching A Dreamer Who Has Nowhere To Land But In The Ocean Of Reality.
-
Apr 10, 2002, 10:45 #3
- Join Date
- Jun 2001
- Location
- rollin' on dubs
- Posts
- 492
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The best way to do this is with a regular expression:
http://www.webreference.com/js/column5/
Unfortunately, I am very bad at them and won't lead you astray with my pitiful attempt to construct one. Some others on this forum can probably post some code for you.
-
Apr 10, 2002, 11:03 #4
- Join Date
- Sep 2001
- Posts
- 94
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks. The length thing worked great. I found this code to check for integer:
<script language="Javascript">
function showData() {
var oldstring = document.formone.inputone.value;
var newstring = parseFloat(oldstring).toString();
var InpValid=1;
if (oldstring.length == newstring.length && newstring != "NaN") {
alert("Input is a number");
}
else {
alert("Input is not a number");
InpValid=0;
}
}
</script>
I won't alert if it is OK, but is this kinda what you were refering to? Hopefully I can get it working.
Thanks.
-
Apr 10, 2002, 22:41 #5
- Join Date
- Aug 2001
- Location
- London
- Posts
- 2,475
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
or this checks each character
PHP Code:
function isInt(textObj)
{
var newValue = textObj.value;
var newLength = newValue.length;
for(var i = 0; i != newLength; i++)
{
aChar = newValue.substring(i,i+1);
if(aChar < "0" || aChar > "9")
{
return false;
}
}
return true;
}
then just use an if statement to check if the integer is between the amount you want
-
Apr 11, 2002, 06:49 #6
- Join Date
- Nov 2001
- Location
- Maryland
- Posts
- 175
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
James -
That code is a lttle weird, but it looks like it will work. Post if you can't get it to work.
AndrewJ -
While your function will work great, it isn't ideal for this situation, since PHP is server-side and form checking is ideal in Javascript, so unnecessary calls to the server aren't made.
Drew~Drew
There Is No Greater Joy Than Soaring High On The Wings Of Your Dreams, Except Maybe The Joy Of Watching A Dreamer Who Has Nowhere To Land But In The Ocean Of Reality.
-
Apr 11, 2002, 07:44 #7
- Join Date
- Aug 2001
- Location
- London
- Posts
- 2,475
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The reason I posted that code was because I thought it would save time on the users side.
What I mean is that its extremely annoying to post a msg to find out you've made a little mistake. With javascript checking on the clientside it makes this much easier to deal with without waiting for that error to come back from php, perl, or asp.
Even if javascript is disabled, on the server side it would be ideal to have the code checked this is purely to do with usability.
Even if you want the page to be rendered differently you could always use the dom to make changes. However you can always influence where you want the page to go by using document.form1.action="my_target_page.php" so its more clientside than serverside.
any way hope that helped
Bookmarks