Number validation in form field

This is a discussion on "Number validation in form field" within the JavaScript Forum section. This forum, and the thread "Number validation in form field are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old May 26th, 2006, 09:42
New Member
Join Date: Jan 2006
Location: Buckinghamshire
Age: 26
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Number validation in form field

Im working thorugh a variety a javascript problems and have managed to crack them - BUT ONE!

The problem is im trying to get a field to validate the number a user puts in it

The number of digits the user puts in will always be 6. However the 1st number must be either a 1 or 2. As 3 and onwards will be used for other sites. Anyone got any suggestions?

Much thanks

Mat
Reply With Quote

  #2 (permalink)  
Old May 26th, 2006, 13:15
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: Number validation in form field

The field content is always treated like a string even if its a number.

Therefors you can use string function to check the length of the number, ie, that it's six digits long and you can check that the sub-string, ie, the first character is one of the required numbers.

You need a combination of:

var checkStr = new String(document.getElementById('fieldID').value);

checkStr.length == 6;

checkStr.charAt(checkStr.0) == 1;

and so forth.
Reply With Quote
  #3 (permalink)  
Old May 26th, 2006, 15:05
New Member
Join Date: Jan 2006
Location: Buckinghamshire
Age: 26
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Number validation in form field

Thanks Geoff

Also just done it this way also, no one can enter a 5 digit code now and it cant be above 29999 also

Quote:
<script language="JavaScript"><!--
function validate(what) {
what.output.value = '';

for (var i=0, len=what.input.value.length-1, valid='0123456789.' ; i<len ; i++) {
if (valid.indexOf(what.input.value.substring(i,i+1)) == -1) {
alert('invalid data');
return;
}
}
number = Math.floor(what.input.value - 0);
if (number > 29999 && number< 300000)
what.output.value = number;

else
alert('Invalid Account Number');
return;
}
function IsEmpty(aTextField) {
if ((aTextField.value.length==0) ||
(aTextField.value==null)) {
return true;
}
else { return false; }
}
//--></script>
Reply With Quote
Reply

Tags
number, validation, form, field

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] playing with Perl Compatible Regex in field validation eon201 JavaScript Forum 33 Oct 26th, 2007 15:07
[SOLVED] Field validation and changing display properties... c_martini JavaScript Forum 12 Sep 25th, 2007 11:27
Non-text field Validation NewDesigner JavaScript Forum 6 Nov 24th, 2006 22:34
date, email and number or character validation ntgcmlfu Classic ASP 5 Jul 30th, 2006 15:32
Field Validation HELP! Monie JavaScript Forum 5 Nov 11th, 2004 19:46


All times are GMT. The time now is 10:44.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43