Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  20] [ 7]  / answers: 1 / hits: 46629  / 13 Years ago, mon, november 14, 2011, 12:00:00

Hello so i'm trying to trim the white spaces when the user enter spaces he get an error explaning that he should enter valid chars but if he used: ' test ' the same value is entered in the database do i need to trim that again in javascript ?



function validateForm()
{
if(trim(document.insert.aname.value) ===)
{
alert(Animal should have a name);
document.insert.aname.focus();
return false;
}
}
function trim(value) {
return value.replace(/^s+|s+$/g,);
}


please can you help ?
the input page is .JSP


More From » validation

 Answers
107

You're not actually trimming the input. You want this:



//store the input in a variable
var nameInput = document.insert.aname

function validateForm() {
//Get the trimmed name
var animalName = trim(nameInput.value)

if(animalName) {
//Update the form with the trimmed value, just before the form is sent
nameInput.value = animalName
}
else {
//Trimmed value is empty
alert(Animal should have a name);
nameInput.focus();
return false;
}
}

function trim(value) {
return value.replace(/^s+|s+$/g,);
}

[#89140] Friday, November 11, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devlin

Total Points: 474
Total Questions: 113
Total Answers: 100

Location: Sweden
Member since Fri, Apr 16, 2021
3 Years ago
devlin questions
Tue, Apr 27, 21, 00:00, 3 Years ago
Sat, Oct 31, 20, 00:00, 4 Years ago
Fri, Aug 28, 20, 00:00, 4 Years ago
;