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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation 3

Status
Not open for further replies.

mcmon999

IS-IT--Management
Mar 3, 2005
21
GB
Hi,

I'm using an example of some validation i found on the internet but it does not allow me to submit Spaces between words.

E.g
i can submit "this_is_a_test" but can not insert "This is a test"

CODE:

<!--
function validateFormOnSubmit(theForm) {
var reason = "";

reason += validateUsername(theForm.comments);
reason += validateEmpty(theForm.comments);

if (reason != "") {
alert("Some fields need correction:\n" + reason);
return false;
}

return true;
}

function validateEmpty(fld) {
var error = "";

if (fld.value.length == 0) {
fld.style.background = 'Yellow';
error = "The required field has not been filled in.\n"
} else {
fld.style.background = 'White';
}
return error;
}

function validateUsername(fld) {
var error = "";
var illegalChars = /\W/; // allow letters, numbers, and underscores

if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter a username.\n";
} else if (illegalChars.test(fld.value)) {
fld.style.background = 'Yellow';
error = "The username contains illegal characters.\n";
} else {
fld.style.background = 'White';
}
return error;
}
//-->

Does anyone know how i can insert spaces between words?

Sorry about this, i'm not very good with JS.

Thanks in advance

 
this routine you are calling on the comments field is for validating a username, and generally usenames don't allow spaces, there is a section for illegal character
Code:
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores

which states what chars are not allowed \W means white space characters i.e. space, return, tab etc..

why are you running the comments field through the username validation routine ?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
1DMF, \W actually means non-word character. It will match white spaces like you mentioned above. But it will also match special characters and punctuation like ! @ # $ % ^ & . ? < > etc...

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Ha oh yeah, i wasn't thinking about that.

I basically just want to check that field for illegal characters and that was the only piece of code the would work.

do you know the function to allow letters, numbers only?

I really appreciate your help on this.
 
lol - oh yeah kaht, have a star you star.

mcmon999, the way i do this is with a sexy regex!

I also do it on keyup and delete as they type!

here's the code i use...

<input type="text" name="comments" onkeyup="check_numchar(this);">

Code:
function check_numchar(obj){
 
 //remove non-number / chars
    val = obj.value;
    val = val.replace(/[^0-9a-zA-Z]/g, ''); // strip illegal characters
    if(obj.value != val){
        obj.value = val; // replace input value 
    }        
    
}

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top