473,378 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.value;
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 3457
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.asp>

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="update(this)"
onkeyup="update(this)"
onkeypress="update(this)">
<span id="bar-info" class="normal">Please enter 15 digits</span>
</form>

<script type="text/javascript">
function update(textField){
if(document.getElementById) {
var infoBox = document.getElementById("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.innerHTML=messages.NORMAL;
infoBox.className="normal";
} else if(/\D/.test(value)) {
// one non digit char has been spotted
infoBox.innerHTML=messages.NUMBER;
infoBox.className="warning";
} else if(value.length!=15){
// Still not 15 digits
infoBox.innerHTML=messages.DIGITS.replace(/xx/,value.length);
infoBox.className="normal";
} else {
// OK
infoBox.innerHTML=messages.VALID;
infoBox.className="valid";
}
}
}
</script>
---
Kind regards,
Elegie.
Jan 9 '07 #2
Tom

"Elegie" <el****@invalid.comwrote in message
news:45*********************@news.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.asp>

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.javascript message <ve*********************@bt.com>, Tue, 9
Jan 2007 10:51:01, Tom <so*****@home.ukposted:
>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.demon.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.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.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
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...
2
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...
12
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
by: Salimunnisaa | last post by:
pls give me code for entering only alphabets in textboxes
4
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...
6
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...
2
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...
1
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...
4
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.