|
-
Apr 21st, 2004, 05:29 PM
#1
Thread Starter
Fanatic Member
Prevent user from entering in the same number in the text box
I want to prevent a user from entering in all of the same number. Example the user enters in all 2, IE: 22222222222
I want to display a msg.
How can I prevent the user from entering in all of the same numbers?
Note the user can enter in the same number more than once but the value in the txt box cannot contain only that one number
-
Apr 21st, 2004, 05:47 PM
#2
Frenzied Member
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function check_this(_obj) {
first_number = _obj.value.substr(0,1)
number_of_them = 0
for (i=0; i<_obj.value.length; i++)
{
if (_obj.value.substr(i,1) == first_number)
{
number_of_them++
}
}
if (number_of_them == _obj.value.length)
{
alert("cannot contain all the same number")
_obj.value = ""
}
}
</script>
</head>
<body>
<input type="text" value="22222" id="mytxtx" onBlur="check_this(this)">
</body>
</html>
Have I helped you? Please Rate my posts. 
-
Apr 22nd, 2004, 03:17 AM
#3
It's quicker if you don't search the entire string.
Code:
var first = value.substr(0,1);
for(var i = 1; i < value.length; ++i) {
if(value[i] != first) {
break;
}
}
if(i == value.length) {
alert("Everything the same");
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 22nd, 2004, 03:18 AM
#4
It's quicker if you don't search the entire string.
Code:
var first = value.substr(0,1);
for(var i = 1; i < value.length; ++i) {
if(value[i] != first) {
break;
}
}
if(i == value.length) {
alert("Everything the same");
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 22nd, 2004, 11:26 AM
#5
Thread Starter
Fanatic Member
Works fine only problem is if you leave the value empty you still get the same err msg. How can I prevent this? This is reference to the first solution by Acidic
-
Apr 22nd, 2004, 11:37 AM
#6
Frenzied Member
just insert if IF like:
Code:
if (_obj.value.length==0)
{
alert("You must enter numbers in this field")
}
else {
//Do the rest
}
But I would use CB solution, it's quicker (and shorter code). You can still use the same IF statement I just gave.
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|