How do i to filter dots in a texbox using javascript
I want to alert the user if he enters only dots without any alphabets or numerals in a textbox using javascript.
If the input contains dots in between alphabets and numerals the javascript should not alert the user.I have a javascript which alerts the user even if dots are present in between alphabets and numerals.
Can any1 Help me..Below is my script:
Code:
function addressValidation(obj)
{
var regex = new RegExp("[.]");
if(obj.value.match(regex))
{
alert("Dot is not allowed");
obj.focus();
return false;
}
}
Last edited by arunkumar93; 09-29-2010 at 06:33 PM..
<script type = "text/javascript">
function addressValidation(obj) {
var regex = /^\./;
obj.value = obj.value.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
// OR IF YOU PREFER obj.value = obj.value.replace(/^[^a-z0-9\.]+/i,""); // strip leading all non-alphanumeric characters (except dots)
if(obj.value.match(regex)) {
alert("Dot is not allowed");
obj.focus();
return false;
}
}
</script>
. in a regex means "any character". A literal dot is \.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.
Philip's trick is a neat one: After getting rid of the leading/trailing spaces, he is simply checking to see if the dot is the first character left. If it is, then it's no good.
So his code *DOES* allow
a.b
3.x
=.=
$.$
and it PREVENTS
.
.b
.6
.=
.$
But it *also* allows
a.
2.
=.
$.
Is that what you want?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
"If the input contains dots in between alphabets and numerals the javascript should not alert the user."
I have made a modification to the code but like you I am unclear if characters other than alpha-numeric are allowed. My alternative deletes them in the start position in the string.
function addressValidation(obj) {
var regex = /(^\s*\.|\.\s*$)/;
if( regex.test( obj.value ) {
alert("Neither leading nor trailing dot is allowed");
obj.focus();
return false;
}
return true;
}
Combined the "strip spaces" so it's all done with one regex.
You are getting tired or your brain is becoming weak.
That will not block .Old Pedant (dot followed by anything except a space). As I understand it the OP's requirement is simple - no leading dot(s). Dots between alphanumeric characters are OK.
In any case there is a typo:-
if( regex.test( obj.value ) ) {
I cannot understand this passion for crowding several different tests into one regex. It simply makes it harder to test and debug.
Just stick with the code I gave you in Post#4, with the following modification if trailing as well as leading dots are not allowed:-
var regex = /^\./;
obj.value = obj.value.replace(/^\s+|\s+$/g,"");
Thanks & Regards
Arun
var regex = /^\./; // ^ means "at the start of the string". \. means "a dot character". So the regex means "match a dot character at the start of the string".
obj.value = obj.value.replace(/^\s+|\s+$/g,""); // means strip (delete or replace by nothing "") all space characters (\s+) at the start (^) or (|) the end ($) of the string. The g switch means "global", that is replace all matches rather than just the first one.
<script type = "text/javascript">
function addressValidation(obj) {
var regex = /^\./;
obj.value = obj.value.replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
// OR IF YOU PREFER obj.value = obj.value.replace(/^[^a-z0-9\.]+/i,""); // strip leading all non-alphanumeric characters (except dots)
if(obj.value.match(regex)) {
alert("Dot is not allowed");
obj.focus();
return false;
}
}
</script>
In this is code can you change regex so it allows anything which starts with dot.