...

View Full Version : javascript form validation - auto remove chars



glish_dreams
06-03-2008, 09:33 PM
Im having an issue with Javascript form Validation

I have a form where a use inputs there phone number
I then submit that do a database, but I dont want spaces hyphens or anything other then a number (0-9)


I have seen it before, but cant find it
Basiclly all I want it to do, is remove any character from the input box other then numbers




I forgot to add
I want it to automaticly remove the characters after they (blur) unfocus from the input field

mrhoo
06-03-2008, 09:41 PM
Remove everything that is not a digit from the field's value.


inputelement.onchange= function(e){
e=window.event? event.srcElement || e.target;
e.value=e.value.replace(/\D/g,'');
}

glish_dreams
06-03-2008, 09:58 PM
thanks!!!!!!!!!



function onlynums(e){
e.value=e.value.replace(/\D/g,'');
}

<input name="nums" onchange="onlynums(nums);" />




I couldnt get it to work with the top string you included
nor was able to understand that part

I new it would be rather simple, I just dont understand the Operators for the replace function

like I have no idea what /\D/g means, well i kinda do

http://us2.php.net/preg-replace

mrhoo
06-04-2008, 12:53 AM
\d any digit (0-9)

\D anything not a digit

the g means replace all matches in the string (global replace)

glish_dreams
06-04-2008, 07:49 PM
\d any digit (0-9)

\D anything not a digit

the g means replace all matches in the string (global replace)


You have no idea how much that actually helps, somehow I can completely understand the replace function.


now, Id like to kick this up a notch if possible.

Is there any way, that if they put letters like (abc) that its converted into the number as if they push A on the phone.
BUT instead of it automaticly doing so
Is there a way where it would prompts
(Letters have been detected, click ok if you would like it converted to a number,, click cancel to remove

abc (2)
def (3)
ghi (4)
jkl (5)
mno (6)
pqrs (7)
tuv (8)
wxyz (9)

mrhoo
06-05-2008, 04:20 AM
// You can do this. One way is to set up a key function that translates any
// sequence of numbers and letters to their 'telephone' codes.


var phonecode= function(str){
var key= {
a:2,b:2,c:2,d:3,e:3,f:3,g:4,h:4,i:4,j:5,k:5,l:5,
m:6,n:6,o:6,p:7,q:7,r:7,s:7,t:8,u:8,v:8,w:9,x:9,y:9,z:9
}
str= str.toLowerCase().replace(/[a-z]/g,function(c){
return key[c];
})
return str;
}

//test
alert(phonecode('javascript'))

glish_dreams
06-06-2008, 10:51 PM
Thank you greatly for your help. One last questions
Is the following correct, and can it be short'en


<html>
<head>
<script type="text/javascript">
<!--
function onlynums(e){
e.value=e.value.replace(/\D/g,'');
}
var phonecode= function(str){
var key= {
a:2,b:2,c:2,d:3,e:3,f:3,g:4,h:4,i:4,j:5,k:5,l:5,
m:6,n:6,o:6,p:7,q:7,r:7,s:7,t:8,u:8,v:8,w:9,x:9,y:9,z:9
}
str= str.toLowerCase().replace(/[a-z]/g,function(c){
return key[c];
})
return str;

}
function letstonums(e) {
var key= {
a:2,b:2,c:2,d:3,e:3,f:3,g:4,h:4,i:4,j:5,k:5,l:5,
m:6,n:6,o:6,p:7,q:7,r:7,s:7,t:8,u:8,v:8,w:9,x:9,y:9,z:9
}
e.value=e.value.toLowerCase().replace(/[a-z]/g,function(c){
return key[c];
})
}
function confirmation(e) {
var answer = confirm("Alphanumeric characters detected, by default they will be converted into vanity numbers.\n Choose cancel if you would like to remove the Alphanumeric characters instead of converting.")
if (answer){
alert("The Alphanumeric characters have been converted into vanity numbers")
letstonums(e);
onlynums(e);
}
else{
alert("Removing the Alphanumeric characters")
onlynums(e);
}
}
//-->
</script>
</head>
<body>
<form>
Enter text into the box, and when you unfocus it will prompt to convert them into Vanity Numbers.<br />
<!-- <input name="nums" onchange="alert(phonecode(nums.value));" /> -->
<input name="lets" type="text" onblur="confirmation(lets)">
</form>
</body>
</html>

lilcleo001
02-03-2009, 12:02 AM
I have a similar issue. I need the form to remove txt and dashes. i only need numbers. i was able to figure out how to remove dashes.

<input type="text" onchange="this.value = this.value.replace (/\W/g, '');">

how do i remove standard text too? I only want numbers? Thanks!

Philip M
02-03-2009, 08:41 AM
<input type="text" onchange= "this.value = this.value.replace (/[^0-9]/g, '')">

i.e. remove anything which is not 0-9

or to "remove at source":-

<input type="text" onkeyup = "this.value = this.value.replace (/[^0-9]/g, '')">



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum