检查输入是否以特定字符结尾

时间:2015-10-27 16:26:47

标签: javascript html input

我有这个基本输入字段:

<input type="text" name="one" />

如何在运行服务器端代码之前确保用户使用特定字符结束输入值,例如逗号(,)?

示例:

  

“你好世界”(无效)

     

“你好世界”,(有效)

8 个答案:

答案 0 :(得分:1)

您可以使用includes()

var str = 'hello world,'
if (str.includes(',',-1)) alert ('valid')

请注意,includes是es6功能,仅适用于现代浏览器,请参阅我上面发布的链接以获取更多信息和填充。

答案 1 :(得分:1)

这可以使用HTML5的输入pattern属性来完成,该属性使用正则表达式验证输入。该表单不允许在现代browsers that support this attribute中提交 - 但您也应始终验证用户输入服务器端。

input:valid {
  background: lime;
}

input:invalid {
  background: red;
}
<input type="text" pattern="(.+?),$">

此示例中使用的CSS纯粹是在输入有效(绿色)时显示。

答案 2 :(得分:1)

为什么不使用HTML5模式验证?它非常简单,无需维护Javascript。

演示代码段

<form>
    <input type="text" name="one" pattern=".*,$" />
    <input type="submit" value="Submit" />
</form>

答案 3 :(得分:0)

HTML:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>     
#include <Eigen/Sparse>
#include <Eigen/Dense>
#include <gsl/gsl_matrix.h>
#include <sys/time.h>
#include <gsl/gsl_blas.h>
#define helix 100
#define rows helix*helix
#define cols rows
#define filling rows/4
#define REPS 10


using namespace Eigen;

/*-- DECLARATIONES --*/
int FillSparseMatrix(SparseMatrix<double> & mat);
int FillDenseMatrices(MatrixXd & Mat, gsl_matrix *testmat);
double vee(int i, int j);
int set_vectors_randomly(gsl_vector * v2, VectorXd v1);

int main()
{
int rep;
    struct timeval tval_before, tval_after, tval_result;

gsl_matrix *testmat     = gsl_matrix_calloc(rows, cols);
gsl_vector *v2      =gsl_vector_calloc(cols);
gsl_vector *prod    =gsl_vector_calloc(cols);

SparseMatrix<double> mat(rows,cols);         // default is column major
MatrixXd Mat(rows,cols);         // default is column major
VectorXd v1(cols), vv1(cols);

FillSparseMatrix(mat);
FillDenseMatrices(Mat, testmat);
    printf("\n/*--- --- --- ---*/\n");
for(rep=0;rep<REPS;rep++)
{
set_vectors_randomly(v2, v1);

    gettimeofday(&tval_before, NULL);       
vv1 = mat*v1;
    gettimeofday(&tval_after, NULL);
    timersub(&tval_after, &tval_before, &tval_result);

    printf("Time for one product, SPARSE EIGEN / secs: %ld.%06ld\n", (long int)tval_result.tv_sec, (long int)tval_result.tv_usec);
    gettimeofday(&tval_before, NULL);       
gsl_blas_dgemv( CblasNoTrans,1.0, testmat, v2, 0.0, prod);
    gettimeofday(&tval_after, NULL);
    timersub(&tval_after, &tval_before, &tval_result);
    printf("Time for one product, GSL / secs: %ld.%06ld\n", (long int)tval_result.tv_sec, (long int)tval_result.tv_usec);

    gettimeofday(&tval_before, NULL);       
vv1 = Mat*v1;
    gettimeofday(&tval_after, NULL);
    timersub(&tval_after, &tval_before, &tval_result);
    printf("Time for one product, DENSE EIGEN / secs: %ld.%06ld\n", (long int)tval_result.tv_sec, (long int)tval_result.tv_usec);
    printf("/*--- --- --- ---*/\n\n");


  //std::cout << mat << std::endl;
}
gsl_matrix_free(testmat);   
printf("--- --- --->DONE\n");
return(0);
}

/*-- --*/
int FillSparseMatrix(SparseMatrix<double> &mat)
{
int i, j;
Eigen::VectorXd Vres;
mat.reserve(Eigen::VectorXi::Constant(cols,filling));

printf("Filling Sparse Matrix ...");
    for(i=0;i<rows;i++)
    {
        if(i%2500==0){printf("i= %i\n", i);}
    for(j=0;j<cols;j++)
        {
        if (vee(i,j) != 0){mat.insert(i,j) = vee(i,j);    /*alternative: mat.coeffRef(i,j) += v_ij;*/ }
        }

    }

return(0);
}
/*-- --*/

/*-- --*/
int FillDenseMatrices(MatrixXd &Mat, gsl_matrix * testmat)
{
int i, j;
Eigen::VectorXd Vres;
double aux;
printf("Filling Dense Matrix ...");
    for(i=0;i<rows;i++)
    {
        if(i%2500==0){printf("i= %i\n", i);}
    for(j=0;j<cols;j++)
        {
        aux = vee(i,j);
        if (aux != 0)
        {
        Mat(i,j) = aux;    
        gsl_matrix_set(testmat, i, j, aux);
        }
        }

    }
return(0);
}
/*-- --*/

double vee(int i, int j)
{
    double result = 0.0;

    if(i%4 == 0){result =1.0;}

    return result;
}
/*-- --*/
int set_vectors_randomly(gsl_vector * v2, VectorXd v1){
printf("Setting vectors rendomly anew ...\n");
for (int j=0;j<cols;j++) 
{
double r=drand48();
v1(j) =r;
gsl_vector_set(v2, j, r);

}
return(0);
}
/*-- --*/

JS:

<form name="myForm">
  <input type="text" name="one" onkeypress="validateForm()">
  <input type="submit">
</form>

答案 4 :(得分:0)

有javascript函数endsWith哪个检查字符串以某个字符结尾

 var chr  = "Hello world,";
    if(chr.endsWith(",") {

    }else{

    }

正如肯尼所说它不会在IE上工作..MDN为这个功能提供了原型功能

if (!String.prototype.endsWith) {
  String.prototype.endsWith = function(searchString, position) {
      var subjectString = this.toString();
      if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
        position = subjectString.length;
      } 
      position -= searchString.length;
      var lastIndex = subjectString.indexOf(searchString, position);
      return lastIndex !== -1 && lastIndex === position;
  };
}

答案 5 :(得分:0)

最基本的方法类似于Marcin C's:

function endsWith(str, end) {
    var $len = str.length,
    $endlen = end.length;
    if (substr($len - $endlen, $endlen) === end) {
        return true;
    } else {
        return false;
    }
}

答案 6 :(得分:0)

我会添加一个ID,以便轻松提取值:

load manosData
for j=1:9
subplot(3,3,j)
end

然后使用正则表达式查看它是否与您的模式匹配:

var textInputValue = document.getElementById("myIdHere").value; 

这是一个小提琴:http://jsfiddle.net/ybsuyx6e/

答案 7 :(得分:-1)

/,$/.test("hello world,") // true