473,611 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

entering a numric only

Tom
Being a newbie to JS, I would appreciate some advice. When a visitor
enters a number into a textbox, I want to check that it is 15 digits long
and then display a message accordingly and the following script does that
OK. However, I want also to make sure that only "numbers" are entered and
no other characters. Again the attached does this but is clumsy to say the
least! How do I get it to loop back to the input box if it contains a
non-numeric, before it goes off to check for 15 digits?

<script>
var ok = " This chip will work";
var nook = "This chip will not work";
function check_input()
{
var entry = document.forms. f.textfield.val ue;
var length = entry.length
document.clear( );
res = isNaN(entry) ? "please enter only numbers": "Press OK to continue";
alert(res);

if(length == 15) document.write( ok);
else
document.write( nook);
}
</script>
Any help would be greatly appreciated.
Tom
Jan 9 '07 #1
3 3467
Tom wrote:

Hello,
When a visitor
enters a number into a textbox, I want to check that it is 15 digits long
and then display a message accordingly and the following script does that
OK. However, I want also to make sure that only "numbers" are entered and
no other characters.
res = isNaN(entry) ? "please enter only numbers": "Press OK to continue";
The isNaN function tests whether the entry is not a number, which
include all sorts of numbers, ranging from positive integers to floats
or numbers expressed using scientific notation. It seems to me that you
want some more restrictive test, accepting only unsigned integers.
if(length == 15) document.write( ok);
By doing this, you will start writing some new page. This isn't probably
what you have in mind, it is actually possible to update the current
document without having to rewrite it.

<URL:http://jibbering.com/faq/#FAQ4_15>
Any help would be greatly appreciated.
The best way to validate text input it to use "regular expressions".
These are expressions which can validate whether a string matches some
pattern. You can learn more about it here :

<URL:http://msdn.microsoft. com/library/default.asp?url =/library/en-us/script56/html/2380d458-3366-402b-996c-9363906a7353.as p>

Here goes some simple example which you might find useful.

---
<style type="text/css">
#bar-info { font-size:0.9em; }
..normal { color:#aaa; }
..warning { color:#c00; }
..valid { color:#0c0; }
</style>

<form action="#" name="foo">
<input
type="text"
maxlength="15"
name="bar"
onchange="updat e(this)"
onkeyup="update (this)"
onkeypress="upd ate(this)">
<span id="bar-info" class="normal"> Please enter 15 digits</span>
</form>

<script type="text/javascript">
function update(textFiel d){
if(document.get ElementById) {
var infoBox = document.getEle mentById("bar-info");
var value = textField.value ;
var messages = {
NORMAL : "Please enter 15 digits",
NUMBER : "Only numbers, please!",
DIGITS : "xx digits (15 required)",
VALID : "Entry validated."
}

if(value.length ==0) {
//field empty
infoBox.innerHT ML=messages.NOR MAL;
infoBox.classNa me="normal";
} else if(/\D/.test(value)) {
// one non digit char has been spotted
infoBox.innerHT ML=messages.NUM BER;
infoBox.classNa me="warning";
} else if(value.length !=15){
// Still not 15 digits
infoBox.innerHT ML=messages.DIG ITS.replace(/xx/,value.length);
infoBox.classNa me="normal";
} else {
// OK
infoBox.innerHT ML=messages.VAL ID;
infoBox.classNa me="valid";
}
}
}
</script>
---
Kind regards,
Elegie.
Jan 9 '07 #2
Tom

"Elegie" <el****@invalid .comwrote in message
news:45******** *************@n ews.free.fr...
Tom wrote:

Hello,
When a visitor
enters a number into a textbox, I want to check that it is 15 digits long
and then display a message accordingly and the following script does that
OK. However, I want also to make sure that only "numbers" are entered
and
no other characters.
>res = isNaN(entry) ? "please enter only numbers": "Press OK to continue";

The isNaN function tests whether the entry is not a number, which include
all sorts of numbers, ranging from positive integers to floats or numbers
expressed using scientific notation. It seems to me that you want some
more restrictive test, accepting only unsigned integers.
>if(length == 15) document.write( ok);

By doing this, you will start writing some new page. This isn't probably
what you have in mind, it is actually possible to update the current
document without having to rewrite it.

<URL:http://jibbering.com/faq/#FAQ4_15>
>Any help would be greatly appreciated.

The best way to validate text input it to use "regular expressions". These
are expressions which can validate whether a string matches some pattern.
You can learn more about it here :

<URL:http://msdn.microsoft. com/library/default.asp?url =/library/en-us/script56/html/2380d458-3366-402b-996c-9363906a7353.as p>

Here goes some simple example which you might find useful.

Thanks a million. this is exactly the info I wanted.
Tom
Jan 9 '07 #3
In comp.lang.javas cript message <ve************ *********@bt.co m>, Tue, 9
Jan 2007 10:51:01, Tom <so*****@home.u kposted:
>Being a newbie to JS, I would appreciate some advice. When a visitor
enters a number into a textbox, I want to check that it is 15 digits long
and then display a message accordingly and the following script does that
OK. However, I want also to make sure that only "numbers" are entered and
no other characters. Again the attached does this but is clumsy to say the
least! How do I get it to loop back to the input box if it contains a
non-numeric, before it goes off to check for 15 digits?
You do not need to do that.

OK = /^\d{15}$/.test(entry)

gives true if & only if your string entry contains exactly 15 decimal
digits.

See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>.

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2886
by: Babs Patel | last post by:
I'm new to database design, but I understand the basics of MySQL and php. To make a simple music database in MySQL, I've made a table of songs (song) and a table of players (player) joined by a bridge table (song_play) to allow many-to-many relationships...eg: create table song (song_id int primary key,
2
2001
by: BerkshireGuy | last post by:
Is there a way to display what the expected format is before entering a control. For instance, if I have the input mask of 99/99/0000;0;#, it shows me ##/##/##, but only when I type the first character. Is there a way to have that display before I type anything?
12
5165
by: Phoe6 | last post by:
The Program Fragment is this: int choice; /* Users Input Command */ .. .. .. printf("Enter a command: "); fflush(stdin); choice = getc(stdin); printf("\n");
3
6380
by: Salimunnisaa | last post by:
pls give me code for entering only alphabets in textboxes
4
5870
by: dirk | last post by:
Hey, I'm new to php and I'm trying to write some php code so that I can insert data into a mysql database using html forms. I've got two text forms and a submit button. When entering data and selecting 'submit' a new record in my database is created. So far so good... But when I enter data in only one content and hit the enter key a new record is created with one empty field. And that something I don't want to happen.
6
2784
by: robintwos | last post by:
Hi I would like to validate the while entering the data in a textbox for numeric values. it means . i would like to throw an error message when the user enters a character value. validation should happen while entering it self . I am looking for the piece of code in VB.NET. Please do help me Thanks
2
1689
by: robertns5411 | last post by:
I have two questions: 1) Say I have a form bound to a table. Assume the user is entering data into a new record at the bottom of the form. Now, suppose the user clicks a button on the form while he's still entering data and hasn't yet saved the record that he is modifying. How can the button's click event procedure test to know that the user is in the middle of entering new data? I've tried testing against the form's recordset's...
1
1929
by: bizt | last post by:
Hi, I have a webpage where Im creating AJAX type requests by loading dynamic pages containg JavaScript into hidden iFrames. The reason I am opting for this method over XmlHttpRequest object requests is because I wish for some of my requests to be over a secure https:// connection and other not so private requests to be made over http:// .. using XmlHttpRequest I am unable to make requests between two domains http/ https from the one...
4
1676
by: Neil | last post by:
Having trouble with inserting a record into a table. It's a list of names. But, for some reason, it won't take a particular name. When a user tries to enter a name into the table, the system hangs (users are using Access 2000 via ODBC linked table). I went into QA (SQL 7) and tried an Insert Into statement, but I got the same result from QA: the system just hung. No error message. After 5 minutes, the query was still trying to execute. ...
0
8097
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8596
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8561
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7038
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4042
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4108
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.