简体   繁体   English

Javascript:验证字段(在表单上),使其只能包含字母,允许1个连字符和1个撇号

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

this is my first time really coding in javascript and I've come to a dead end on my part. 这是我第一次真正用JavaScript进行编码,而我已经走到了尽头。 I need to make a function (no RegExp allowed unfortunately) that allows my pizza form to validate a user's input. 我需要做一个函数(不幸的是没有RegExp允许),使我的披萨表单可以验证用户的输入。 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. 并且,一旦用户单击提交给cgi的按钮,我的最终结果就是在所有大写字母中显示名称。

example; 例;

enter client name: mike or m'ike or m'i-ke 输入客户名称:mike或m'ike或m'i-ke

(click submit pizza order) (点击提交披萨订单)

result: MIKE or M'IKE or M'I-KE. 结果:MIKE或M'IKE或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 这个函数将在我的主要函数中被调用,以弹出一个窗口,显示所有错误(如果在if语句中创建,则可以工作),在我的else语句中有此错误

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. 因此,现在发生的情况是,如果输入了小写字母,表格将不接受它,但是如果输入了大写字母,则表格将接受它,并在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()... 我不知道如何解决这个问题,用户可以输入小写字母,它应该接受,但是在cgi中,它应该更改为大写字母,这就是为什么我要使用.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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript正则表达式:只允许英文字母 - javascript regex : only english letters allowed 我可以使用HTML5 API针对允许值列表动态地验证表单字段值吗? - Can I use the HTML5 API to validate a form field value dynamically against a list of allowed values? 需要Javascript正则表达式以防止用户仅输入字符且仅允许使用单引号 - Need Javascript Regex to prevent user to enter only characters and only one apostrophe allowed 仅允许使用Javascript验证域名字符串。.com和.net - Validate domain name string with Javascript only .com and .net allowed 脚本src =中不允许使用连字符? - hyphen not allowed in script src=? Javascript 验证仅允许使用 Alpha 字符、连字符 (-)、点 (.)、撇号 (&#39;) 和空格 - Javascript validation to allow only Alpha characters, hyphen (-), dot (.), apostrophe ('), and space 在Javascript中,如何检查字符串是否只是字母+数字(允许下划线)? - In Javascript, how do I check if string is only letters+numbers (underscore allowed)? 允许带撇号的字母数字字符串模式 - Alphanumeric string pattern with apostrophe allowed 用Javascript进行表单验证。 我想验证一个字段,使其仅接受“ KK-10-HUH”形式的汽车注册 - Form Validation in Javascript. I want to validate a field so that it only accepts car registrations in the form of “KK-10-HUH” 如何在 Javascript 中读取具有固定路径的本地文件? 我只有文件的位置作为字符串。 不允许使用 HTML 表单 - How can I read a local file with a fixed path in Javascript? I only have the location of the file as a string. NO HTML forms allowed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM