简体   繁体   English

使用JavaScript验证在文本框中输入的字符串

[英]Using javascript to validate string entered in textbox

I am working on a search box to search users of my webapp. 我正在使用搜索框搜索我的Web应用程序的用户。 I want the user to enter the firstname and lastname of the user. 我希望用户输入用户的名字和姓氏。 I want to validate the user and make sure that user enters two string parts viz. 我想验证用户并确保用户输入了两个字符串部分。 firstname and lastname in the textbox. 文本框中的名字和姓氏。 If not done so ...i want an error to be thrown. 如果没有这样做...我想抛出一个错误。 Please help with examples. 请提供示例。 I m new to javascript . 我是javascript新手。 My webapp is jsp/servlet based. 我的webapp基于jsp / servlet。

see this example in jsfiddle. 请参阅jsfiddle中的示例。

http://jsfiddle.net/YrFfG/ http://jsfiddle.net/YrFfG/

 $(document).ready(function() {
    $("#btn").click(function(e) {
    if (/\w+\s+\w+/.test($("#cname").val())) {
        alert("good");
    } else {
        alert("bad");
    }

    e.preventDefault();
  });
});

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

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