简体   繁体   中英

Using javascript to validate string entered in textbox

I am working on a search box to search users of my webapp. 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 . My webapp is jsp/servlet based.

see this example in jsfiddle.

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();
  });
});

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