简体   繁体   中英

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/

Some users submit an ID that has only numbers in it (eg 981734844). And some users submit an ID that has underscore "_" (without quotes) in it (eg 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.

In other words:

82174363278423: Don't allow to process the form action

21489724893249_2918423: Allow to process the form action

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/

you need to use java script validation and in that you can use following code:

  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.

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

// 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). 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. Either way, javascript has a 'search' method on strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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