...

View Full Version : how to use javascript to valida a text field value is a text



xiaodao
01-02-2007, 07:34 PM
hi

i have a textfield i want the input be words instead of numbers and symbols

how to do it?

please help

Mr J
01-02-2007, 07:56 PM
Try this snippet


<script type="text/javascript">
<!--
function chkChar(obj){
obj.value = obj.value.replace(/[^a-z\s]/gi,'')
}
// -->
</script>

<input type="text" value="" onkeyup="chkChar(this)">

Philip M
01-02-2007, 08:02 PM
<HEAD>
<SCRIPT type="text/JavaScript">
function validate(f) {
if (/[^A-z\s]/gi.test(f.value)) {
alert("Only alpha characters and spaces are allowed!");
f.value = f.value.replace(/[^A-z\s]/g,"");
}
}
</SCRIPT>
</HEAD>
<BODY>
<input type="text" onkeyup="validate(this)">
</BODY>

There is a typo in Mr J's version:-

function chkChar(obj){
obj.value = obj.value.replace(/[^a-z\s]/gi,'')
}

Mr J
01-02-2007, 08:21 PM
I think it was because I used the php tags it lost the backslash


There should be a backslash between here \ and here

See its gone and lost it again :D

So I put my previous post in code tags

xiaodao
01-03-2007, 12:33 AM
if i also disallow space how

Philip M
01-03-2007, 08:46 AM
remove \s in both versions:-

<HEAD>
<SCRIPT type="text/JavaScript">
function validate(f) {
if (/[^A-z]/gi.test(f.value)) {
alert("Only alpha characters are allowed!");
f.value = f.value.replace(/[^A-z]/g,"");
}
}
</SCRIPT>
</HEAD>
<BODY>
<input type="text" onkeyup="validate(this)">
</BODY>

Or in Mr J's version:-

function chkChar(obj){
obj.value = obj.value.replace(/[^a-z]/gi,'')
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum