entering a numric only

T

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
 
E

Elegie

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.

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/d...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>
 
T

Tom

Elegie said:
Tom wrote:

Hello,



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.


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.



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/d...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
 
D

Dr J R Stockton

Tue said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top