Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate Text Box 1

Status
Not open for further replies.

istreet

Instructor
Jun 24, 2002
9
0
0
GB
I am trying to validate a text box. Only certain characters are allowed in the text box (The code is below). However this code will not allow the user to enter a white space between words:

My House
My Home

I have tried numerous variations (below) but I can't stand it anymore. It's driving me crazy.

[^A-Za-z0-9\s]
[^A-Za-z0-9/\s]
[^A-Za-z0-9/\s*]

What am I missing? Can anybody help.


function validChara(formField,fieldLabel){

var result = true;

var charas = formField.value.search("[^A-Za-z0-9]");
if(formField.value.length > 0 && charas >= 0) {
alert('Please do not use Special Characters in your ' + fieldLabel +'');
formField.focus();
result = false;
}
return result;
}
 
Lrnmore

Hi thanks for your help.

Just used your script but now everything and anything I input comes up

Wrong Char's Entered..
 
Umm,

Let me give you the working model so you can check for
differnces in your code.

Code:
<html><head><title>TEST</title>
<script language="JavaScript">
function ckValue(str){
var re = /[^A-Z0-9 ]/ig;
result = re.test(str);
       if(result) {
       alert("Wrong Char's Entered..");
       }
       else {
       alert("Entry Correct..");
       }
}
</script></head><body>
<input type="text" size="20" onBlur="ckValue(this.value)">
</body></html>

Here's one using search:

Code:
<html><head><title>TEST</title>
<script language="JavaScript">
function ckValue(str){
var re = /[^A-Z0-9 ]/ig;
result = str.search(re);
       if(result > -1) {
       alert("Wrong Char's Entered..");
       }
       else {
       alert("Entry Correct..");
       }
}
</script></head><body>
<input type="text" size="20" onBlur="ckValue(this.value)">
</body></html>

And check out the link below for "Regular Expressions"


Great Javascript Resource:
 
Lrnmore

Hi,

Thanks for all your help, sorted now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top