禁用输入字段中的符号和非字母

时间:2015-09-03 00:32:39

标签: javascript forms input field

我想禁用所有符号输入到与以下内容无关的输入字段:字母或数字或空格或符号符号或句号。

E.g。 允许:允许A-Z,a-z,0-9,& ,.和空格。

不允许:其他所有字符,例如! @#$%^ *() - + = []; :' " < > ,/? | =`〜等等。

<input id="ItemName" type="text" />

4 个答案:

答案 0 :(得分:1)

您可以注册一个按键事件处理程序,如果您不“喜欢”新输入,则返回false:

$('#ItemName').keypress(function (e) {
    var txt = String.fromCharCode(e.which);
    if (!txt.match(/[A-Za-z0-9&. ]/)) {
        return false;
    }
});

JSFiddle:https://www.npmjs.com/package/dynamodb-marshaler

请注意,此解决方案需要JQuery

答案 1 :(得分:0)

正确的方法是使用“输入”事件。 document.addEventListener('input', script); https://developer.mozilla.org/en-US/docs/Web/Events/input

答案 2 :(得分:0)

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        self.searchResults = [] //this might be causing the crash
        print("searchText \(searchText)")
        let searchRequest = MKLocalSearch.Request()
        searchRequest.naturalLanguageQuery = searchText
        
        let search = MKLocalSearch(request: searchRequest)
        search.start { response, error in
            guard let response = response else {
                print("Error: \(error?.localizedDescription ?? "Unknown error").")
                return
            }

            for item in response.mapItems {
                self.searchResults.append(item.placemark)
                self.tableView.reloadData()
            }
        }
    }

}

//MARK: TableView
extension LocationSearchViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResults.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ResultCell", for: indexPath)
        let listItem = searchResults[indexPath.row]
        cell.textLabel?.text = listItem.name
        cell.detailTextLabel?.text = listItem.administrativeArea ?? ""

        return cell
    }
    

}

extension LocationSearchViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("selected a row")
        let local = searchResults[indexPath.row]
 
        // pass local back
        if let delegate = delegate{
            delegate.doSomethingWith(data: local)
        }
        
        
        dismiss(animated: true, completion: nil)
    }
}

答案 3 :(得分:0)

是的,是的,我知道。这个问题太老了。但我只是尝试一下(没有jQuery)

HTML

<input type="text" placeholder="Try to put a non-alphabetical character here! (you can put a number and a space too)" id="nochars" />

JS

const input = document.getElementById("nochars"); // gets the element (the input) by it's id
input.addEventListener("input", (event) =>  {
  const char = String.fromCharCode(event.keyCode)); // changes the keycode from a int to a string
  if (!(/[a-zA-Z0-9\s\.$]/.test(char))) {
    event.preventDefault(); // prevents the default (which is adding the character to the value)
  }
});

还要检查EventTarget.addEventListener does。 (\s是空格-一个空格)