ARC_UM
asked on
Textbox to allow 3 letters & 4 numbers
Hi,
I need some javascript code that can restrict input to a textbox to only allow the following):
-first 3 characters can be only A to Z (both lower & upper caps), or can also have either of the characters: - (hiphen) and ' (single quote)
-next 4 characters can be only integers from 0 to 9
-nothing else is allowed, not even spaces.
Thanks in advance
I need some javascript code that can restrict input to a textbox to only allow the following):
-first 3 characters can be only A to Z (both lower & upper caps), or can also have either of the characters: - (hiphen) and ' (single quote)
-next 4 characters can be only integers from 0 to 9
-nothing else is allowed, not even spaces.
Thanks in advance
I wrote this up then there was an outage and I couldn't post it. This should help:
HTML:
<input id="txtBox" type="text" maxlength="7" />
<input type="button" value="Validate" onclick="validateMe();" />
Javascript:
<script type="text/javascript">
function validateMe()
{
var pattern = /[a-zA-Z]{3}[0-9]{4}/;
var txtBox = document.getElementById('txtBox');
if(txtBox.value.match(pattern) == null)
{ alert('wrong-entry'); }
else
{ alert('correct-entry'); }
}
</script>
Don't forget to put ^ at the beginning, $ at the end and [-'] in the middle.
Use my pattern and ddayx10's code.
Use my pattern and ddayx10's code.
Ahhhh I see I didn't read carefully enough, good catch!
Regular Expressions make my head hurt! :)
Regular Expressions make my head hurt! :)
Maybe I'm misinterpreting the requirement, but the description to me indicates the pattern should be as below--although I think it's a toss-up between this and what was already posted :)
^[a-zA-Z'-]{3}\d{4}$
You may be right. The author should clarify.
Are the - or ' optional?
Do they replace one or more of the letters?
Only one? Up to three?
Do they separate the letters from the numbers?
Is the number of characters always 8? 7? Can it vary?
Are the - or ' optional?
Do they replace one or more of the letters?
Only one? Up to three?
Do they separate the letters from the numbers?
Is the number of characters always 8? 7? Can it vary?
Try this one:
^[A-Z|a-z]{3}['|-][0-9]{4} $
This should do it.
^[A-Z|a-z]{3}['|-][0-9]{4}
This should do it.
@Shahan_Developer
How is that any different than shadow77's original post? Besides, the pipe ( | ) inside of a bracket is redundant... a bracket expression implicity means an OR between all characters.
How is that any different than shadow77's original post? Besides, the pipe ( | ) inside of a bracket is redundant... a bracket expression implicity means an OR between all characters.
kaufmed's post (four back) throws the whole question open. He points out that there are multiple potential interpretations of the original problem, so we need ARC_UM to clarify the requirements.
The pipe inside the brackets is worse than redundant. It will be treated as one of the characters that can be matched at that point (ie, match ' or | or -).
The pipe inside the brackets is worse than redundant. It will be treated as one of the characters that can be matched at that point (ie, match ' or | or -).
>> The pipe inside the brackets is worse than redundant. It will be treated as one of the characters that can be matched...
Agreed.
Agreed.
@ kaufmed
I donot follow any expert for my post. I just read the question and try to solve. If my post would be the repeated one the author will reject it.
I donot follow any expert for my post. I just read the question and try to solve. If my post would be the repeated one the author will reject it.
Don't quite understand your logic, but hey, if it works for you, then I guess all is well. :)
ASKER
input can only be seven characters of whic:
-first 3 can be lower & upper caps A to Z or - or '
-last 4 can only be digits from 0 to 9
which regular expression is correct?Thanks
-first 3 can be lower & upper caps A to Z or - or '
-last 4 can only be digits from 0 to 9
which regular expression is correct?Thanks
kaufmed is correct (nine comments up).
ASKER
doesn't look like it's working.
the reg expr is not to allow users to input anything apart from the pattern & if they type something else it will keep giving them warning msgs. Can you help please?
the reg expr is not to allow users to input anything apart from the pattern & if they type something else it will keep giving them warning msgs. Can you help please?
<script type="text/javascript">
function validateMe()
{
var pattern = ^[a-zA-Z'-]{3}\d{4}$;
var student= document.getElementById('student');
if(student.value.match(pattern) == null)
alert('wrong-entry');
else
alert('correct-entry');
}
</script>
I'm rusty on my JavaScript.
I think you need to quote the pattern and the hyphen needs to come first in the brackets so it isn't interpreted as part of a range (like [a-z]).
var pattern = '^[-a-zA-Z']{3}\d{4}$';
I think you need to quote the pattern and the hyphen needs to come first in the brackets so it isn't interpreted as part of a range (like [a-z]).
var pattern = '^[-a-zA-Z']{3}\d{4}$';
ASKER
The expr is still not working.
Try
var pattern = /^[-a-zA-Z']{3}\d{4}$/;
var pattern = /^[-a-zA-Z']{3}\d{4}$/;
You may need to escape the '
var pattern = /^[-a-zA-Z\']{3}\d{4}$/;
Sorry, my Javascript is rusty.
var pattern = /^[-a-zA-Z\']{3}\d{4}$/;
Sorry, my Javascript is rusty.
ASKER
i think i got the reg expression correct but it's the condition that's not working.
it always shows wrong entry & doesn't delete the wrong entered character
it always shows wrong entry & doesn't delete the wrong entered character
How about something like this?
<script type="text/javascript">
function validateMe()
{
var re = new RegExp(/^[-a-zA-Z\']{3}\d{4}$/);
var student= document.getElementById('student');
if (student.value.search(re) < 0) {
alert('wrong-entry');
else
alert('correct-entry');
}
</script>
ASKER
it doesn't work
Can you post the test strings you are using?
Have you confirmed that student.value holds the strings you want to check? Perhaps you could add it to your alerts temporarily.
I don't have a way to test this right now so I need to ask you.
Have you confirmed that student.value holds the strings you want to check? Perhaps you could add it to your alerts temporarily.
I don't have a way to test this right now so I need to ask you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have used:
Li-0000
O'C9999
no matter what i type, previously it was saying they wre all incorrect & now its doesn't do anything & lets me type
Li-0000
O'C9999
no matter what i type, previously it was saying they wre all incorrect & now its doesn't do anything & lets me type
I tested those two strings against the pattern and both matched. I also made several small changes and the changed strings all failed.
There must be something wrong in the function.
Are you sure that student.value holds the strings you want to check? Might it be student.text?
Maybe you should try
if (re.test(student.value))
alert('correct-entry');
else
alert('wrong-entry');
You should also look at what ddayx10 has posted.
There must be something wrong in the function.
Are you sure that student.value holds the strings you want to check? Might it be student.text?
Maybe you should try
if (re.test(student.value))
alert('correct-entry');
else
alert('wrong-entry');
You should also look at what ddayx10 has posted.
In response to post #30888868, the hyphen can be placed either after the opening bracket or before the closing bracket when including hyphen inside of a character class :)
It would be helpful to all if you could post the relevant code (event handlers and scripts) you are using. Saying that something doesn't work without providing insight as to how you have implemented your code makes our job rather difficult :)
It would be helpful to all if you could post the relevant code (event handlers and scripts) you are using. Saying that something doesn't work without providing insight as to how you have implemented your code makes our job rather difficult :)
@kaufmed
You are correct. I was mistaken. I also tested this version
^[a-zA-Z-']{3}\d{4}$
and it worked too. Apparently, the re processor recognizes that a hyphen following A-Z cannot be part of a range.
You are correct. I was mistaken. I also tested this version
^[a-zA-Z-']{3}\d{4}$
and it worked too. Apparently, the re processor recognizes that a hyphen following A-Z cannot be part of a range.
ASKER
@ ddayx10: almost exactly what i was looking for. should have made clear about the keystroke part.
thanks heaps
thanks heaps
ASKER
@ ddayx10: hi, when i move from that textbox to the next one, it still shows error when i start typing on the new textbox although the previous ones input is correct
ASKER
@ ddayx10: hi, when i move from that textbox to the next one, it still shows error when i start typing on the new textbox although the previous ones input is correct
ASKER
@ ddayx10:
it checks but then the users cant write in other parts of teh folder as the error msg keeps coming.
can you plz help?
it checks but then the users cant write in other parts of teh folder as the error msg keeps coming.
can you plz help?
Oh man I feel aweful. Once I saw it was resolved I took this off monitoring. There is no way to turn it back on. If you still need help of course I will look at it again. I'll check back in the next few days and see if you respond. Again really sorry. I dont check the emails too often because I get too many....
ASKER
the code you posted works to some extent. it checks the user input and doesn't let a user to put in anything else. but when the user move to the next field, even though they've entered this field correctly -- the check wouldn't let the user to move to teh next field and keeps coming back here with error msg.
it would be great if you can help. thanks
it would be great if you can help. thanks
^[a-zA-Z]{3}[-']\d{4}$