简体   繁体   English

不要处理 <form> 如果 <input> 不包含下划线

[英]Don't process the <form> if the <input> DOESN'T contain an underscore

Form: 形成:

<form action="goto.php" method="post" name="myform" id="myform" onsubmit="return formSubmit(this);" class="form-wrapper cf">
  <input name="statusid" autocomplete="off" id="statusid" placeholder="Enter ID Here" type="text">
  <input name="submit_btn" class="submit" id="submit_btn" value="" placeholder="submit" >
</form>

Demo - http://jsfiddle.net/FEF7D/4/ 演示-http://jsfiddle.net/FEF7D/4/

Some users submit an ID that has only numbers in it (eg 981734844). 一些用户提交的ID中仅包含数字(例如981734844)。 And some users submit an ID that has underscore "_" (without quotes) in it (eg 28371366_243322). 并且某些用户提交的ID带有下划线“ _”(不带引号)(例如28371366_243322)。

What I want to do is NOT allowing the users that submit an ID with ONLY numbers in it (eg 89172318) to process the form action. 我想做的是不允许提交ID仅包含数字的用户(例如89172318)来处理表单操作。

In other words: 换一种说法:

82174363278423: Don't allow to process the form action 82174363278423:不允许处理表单操作

21489724893249_2918423: Allow to process the form action 21489724893249_2918423:允许处理表单操作

Is it possible to do that? 有可能这样做吗?

Thanks in advance. 提前致谢。

Try this... 尝试这个...

function formSubmit(form) {
    // create a regex to test for the numbers + underscore + numbers pattern
    var rx = /^\d+_\d+$/,
        test = rx.test(form.statusid.value);

    test || alert('You need to enter an ID with underscore in it.');
    return test;
}

Demo - http://jsfiddle.net/FEF7D/7/ 演示-http://jsfiddle.net/FEF7D/7/

you need to use java script validation and in that you can use following code: 您需要使用Java脚本验证,并且可以使用以下代码:

  if ( String.indexOf('_') != -1 ) {
        // your form processing code..
  }

Yes you can do that. 是的,你可以这么做。 There are a number of solutions. 有许多解决方案。 For example, you can split your input string into an array using the underscore as the split divider and if your array has two entries, you know you've got a single underscore. 例如,您可以使用下划线作为拆分分隔符将输入字符串拆分为一个数组,如果您的数组有两个条目,则说明您只有一个下划线。 eg Something like this (untested snippet to illustrate - you also need to add numeric checks for each part of the array so you probably want to assign the split to a variable if going with this approach): 例如这样的事情(未经演示的片段进行说明-您还需要为数组的每个部分添加数字检查,因此如果采用这种方法,您可能希望将拆分分配给变量):

$('form').submit(function(event)
{
    if (myinputvar.split('_').length ! = 2)
    {
        event.preventDefault();
        return false;
    }
    // do the rest of your submission here
});

You can do any form validation in onsubmit of form. 您可以在表单的onsubmit中进行任何表单验证。

HTML 的HTML

<form name="myform" id="myform" onsubmit="return formSubmit(this);" class="form-wrapper cf" action="goto.php" method="post">
  <input name="statusid" autocomplete="off" id="statusid" placeholder="Enter ID Here" type="text">
  <input type="submit" name="submit_btn" class="submit" id="submit_btn" value="" placeholder="submit" >
</form>

JavaScript 的JavaScript

// Do validation of form
function formSubmit(formObj) {
    // Only allowable character is numeric or underscore 
    if (!/^[_0-9]*$/.test(formObj.statusid.value)) {
        return false;
    }
    return true;
}

The two answers above will work (just strip out Neil's jquery). 上面的两个答案将起作用(仅去除Neil的jquery)。 Another option is 另一种选择是

 function formSubmit(form){ var statusid=document.getElementById("statusid"); if(statusid.value.search("_")===-1){ alert('ID must have an underscore "_".'); } }; 

unfortunately, for some reason I can't get that to work with jsfiddle, it's telling me formSubmit is not a function , but clearly it is. 不幸的是,由于某种原因,我无法使它与jsfiddle一起使用,它告诉我formSubmit is not a function ,但显然是。 Either way, javascript has a 'search' method on strings. 无论哪种方式,javascript都可以对字符串使用“搜索”方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM