Javascript: Validate field (on a form) so that it can only have Letters, 1 hyphen allowed, and 1 apostrophe allowed

StackOverflow https://stackoverflow.com/questions/10046355

質問

this is my first time really coding in javascript and I've come to a dead end on my part. I need to make a function (no RegExp allowed unfortunately) that allows my pizza form to validate a user's input. To make sure it ONLY has letters (no numbers, no spaces, no special characters aside from hyphen and apostrophe).

The user may use ONE apostrophe (with a letter before and after it). and the user may use ONE hyphen (with a letter before it and after it), the user may also use an apostrophe and hyphen at the same time as long as separated by a letter. And once the user clicks submit button to cgi, my end result is to display the name in all caps.

example;

enter client name: mike or m'ike or m'i-ke

(click submit pizza order)

result: MIKE or M'IKE or M'I-KE.

so far this is what I have:

function validatefield01(errMessages)
{


// Get the content of fieldOne

      var entry1_info   = document.pizza.field01.value;                        


// error condition  will show if character count is less than 4 or greater than 15 

      if ((entry1_info.length <4) && (entry1_info.length <15))                

// error message to be displayed

      {
        errMessages += "    <li>Minimum characters for Client Surname is 4 maximum 15\n";
errMessages += "        must use characters only, Apostrophe ( ' ) or hyphen (-) is acceptable.</li>\n";
      }



      return errMessages;

      for(var i=0; i<entry1_info.length; i++)
      {
             ch = entry1_info.length.charAt(i);
             if(ch !=' ' && ch !='.') return false;

      }

to better explain what's going on here. This function will be called in my main function to popup a window displaying all the errors should any have been made (created in an if statement, which does work), in my else statement i have this

else {

       field01=field01.substring(0).toUpperCase();

        return true;                    // No errors - return to browser and submit form
      }
   }                                    //  End of main function

so what's happening right now is, if lower case letters are entered the form wont accept it, but if letters are entered in caps the form will accept it and it will show the name in the cgi. I don't know how to fix that, the user can enter in lowercase and it should accept but in the cgi it should be changed to uppercase, which is why i have that .toUpperCase()...

thank you for the help!

役に立ちましたか?

解決

function validatefield01(errMessages){

var entry1_info = document.f1.sDate.value

    //if character count is less than 4 or greater than 15.
if ((entry1_info.length < 4) || (entry1_info.length > 15)) {

    // error message to be displayed
    errMessages += "    <li>Minimum characters for Client Surname is 4 maximum 15\n";

    return false;

}

var word = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz-'";

    // Check input string validation.
    for ( var i = 0; i < entry1_info.length; i++) {
        if (word.indexOf(entry1_info.charAt(i), 0) == -1) {

        // error message to be displayed.
        errMessages += "must use characters only, Apostrophe ( ' ) or hyphen (-) is acceptable.</li>\n";

        return false;
        }
    }

document.f1.sDate.value = entry1_info.substring(0).toUpperCase();
return true;

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top