Click to See Complete Forum and Search --> : leading spaces in textbox


arrow108
08-21-2006, 10:11 AM
I have a textarea that takes a max of 5ooo characters. If the user begins typing anywhere except for the absolute left of the textarea the form will not submit. i used a trim function in the if/else statement validating the form but i'm thinking that is incorrect because it isn't working as expected. here's the trim function -
function trim(s) {
while (s.substring(0,1) == ' ') {
s = s.substring(1,s.length);
}
while (s.substring(s.length-1,s.length) == ' ') {
s = s.substring(0,s.length-1);
}
return s;
}

here's the size validation contained w/in a lengthy form validation function too long to reprint here -
else if (trim(form.message.value) < "!" )
{
alert("Please enter your message.");
form.message.select();
form.message.focus();
return false;
}

So, when the user enters a message one line down (i.e., presses the hard return before typing) they get the alert box asking them to enter their message.

How can i fix this?

Kor
08-21-2006, 10:24 AM
Can you clarify a bit? Your textarea has already a value and you don't want the user to type anything except after the default text? Or?

arrow108
08-21-2006, 10:57 AM
no. the textarea is initally empty. to submit the form the user must add comments using a max of 5000 characters. if the user fails to enter text on the first line of the input type textarea they receive an alert box message error, 'Please enter your message'. i want this alert box message to show only if the user tries to submit the form with the textarea empty.

Kor
08-21-2006, 02:34 PM
it is to be done using RegExp...hey, you all coders, who are watching this, take it, please, like a contest (or maybe you have already have succh of) !

Yes, it can be done, let's see, pards, whose code is better...

arrow108
08-21-2006, 02:46 PM
this is how i solved the problem:

String.prototype.trim = function() {
return this.replace(/^\s*|\s*$/g, "");
}


function trim(s) {
return s.trim();
}

i modified the original trim function to the above one using RegExp then included the trim function in the form validation function in the if/else statement where it validates the textarea. the credit goes to my co-worker.