prevent characters in text box in php using jquery - php

I want to prevent user to input few characters like ; and \ and /. I now it need jquery to do, but i cant find one
The jquery will used in name. zip code input to prevent such characters etc.
please show me an example as i new to jquery and php

You can use event.which to getting code of pressed key and use return false to preventing enter of character.
$("input").keydown(function(e) {
if (e.which === 186 || e.which === 191 || e.which === 220){
console.log("This character can't entered");
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input />

This should do the job. I hope you can read this simple code:
// select css classes 'input' and assign onKeyDown event to them
$(".input").keydown(function(event) {
// check what key is pressed. dot is not allowed in this case
if ( event.keyCode === 190) {
// prevent event to happen
event.preventDefault();
}
});
On the bottom of this page there is a small table for some codes and also an input field which shows you key code for particular key. Just as a helper, to let you find codes for wished key faster.

Related

How to assign value to .click event

I have many input field that post data when enter key is press i have done one thing that i find id to of selected button and want to pas with click event bt it gives error here is code
$(".text").bind("keydown", function(event) {
// track enter key
var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
if (keycode == 13) {
var id=$(this).parent().find("input[type=submit]").attr('id');
(id).click(); // this one give error
return false;
} else {
return true;
}
});
(id).click(); this line gives error that id.click is not a function.what i m missing.
I think you are looking for:
$(this).closest("form").find("input[type=submit]").trigger("click");
There is no need to try to find the id and create a different JQuery object. You will have a reference to what you are looking for with find("input[type=submit]").
In order to know for sure, we would need to see your markup in order to find the most effective way to find what element you are looking for,
id is a string.
Strings don't have a click method.
Instead, you should submit the form directly:
$(this).closest('form').submit();
You need to use the following:
$("#"+id).trigger("click");

Use JavaScript to prevent spacebar?

I have a live search on a site I'm developing. At the moment, it searches the MySql database after the first character is typed, and updates the search for each new character. When Space is pressed as the first character, it displays all entries in the database. I don't want it to do that. I have the following code that I found somewhere that prevents the SPACE character from being typed:
$('input[type="text"]').keydown(function(e){
var ignore_key_codes = [8,32];
if ($.inArray(e.keyCode, ignore_key_codes) >= 0){
e.preventDefault();
}
});
This does what it's meant to do, but not exactly what I want. This will prevent the space bar from working in the text input at all, what I require is that it only prevents the space bar if it's the first character being typed. For example, typing " apples" would prevent the space, but typing "apples oranges" wouldn't.
Is there anything I can try to achieve this?
You can do it easily like this example:
function keyPress(e) {
var e = window.event || e;
var key = e.keyCode;
//space pressed
if (key == 32) { //space
return;
}
searchInDataBase(); //make your ajax call to search in data base etc.
}
If the space key is pressed, you return and dont do nothing, otherwise you continue in your search...
$('input[type="text"]').keydown(function(e){
if (e.keyCode == 32 && $.trim($(this).val()) == ''){
e.preventDefault();
} else {
alert("searching..");
}
});
Demo: http://jsfiddle.net/BerkerYuceer/JJG9M/
Following on from #Florent's comment, when a user types a value into your textbox,check if the trimmed value is != '' if so it should be safe to continue
e.g
if($.trim($txtbox.val()) != ''){ // proceed to do search
}

Disable zend form element Enter keypress event

Does anyone know how to disable the Enter keypress event of a zend element text input control? When a user presses Enter on a form element it tries to post, which I want to disable.
This is something you'll need to do client side (i.e. with Javascript). With Dojo it would be something like:
dojo.connect(dojo.byId('FIELDID'), 'onkeydown', function(event){
if (event.keyCode == dojo.keys.ENTER) {
dojo.stopEvent(event);
}
});
replace 'FIELDID' with the ID of the text field you want to hook this into. You could combine this with a dojo.query call if you want to apply it to everything in the form.
jQuery and the other JS frameworks will have an equivalent.
I think this Should solve your problem
$textField = new Zend_Form_Element_Text("text");
$textField->setAttrib('onkeypress', 'nullifyEnterKey();');
function nullifyEnterKey(e)
{
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
return (key != 13);
}

Limiting range of input field

Though it is a very common question. I have one input field in which the data entered must be between 1 and 150.
Problem is I am already using some validations on that. That validation is being used by many other input fields. So I cannot change that. It is the num() function.
On one field mesure I want to add extra functionality. I tried it with validate.
I don't know how to merge these two validations for only one input field using both function in document.ready.
I can do it in either jQuery or PHP.
<input type="text" name='mesure' class="numbersonly" maxlength="3" onblur =" validate()"/>
function num() {
$(".numbersonly").bind('keypress', function(e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
$("#errmsg").html("numbers please");
return false;
}
});
}
function validate()
{
if( document.form.mesure.value >150 )
{
alert( "out of range!" );
document.form.mesure.focus() ;
return false;
}
}
Why not using onkeypress/onkeyup, check it using the validate function.
But remember that javascript is not the real solution for limitation - People can turn of or change the javascript. Also use php for checking the value.
For example, if your input field that you want to limit has the id #text, you can use
if($('#text').val().length>150){
//do stuff here
}
As you will probably post the entered value to some place in the backend, the real validation must happen in php. There, you can do something like
<?php
if(strlen($_POST['text'])>150){
echo "too long";
exit;
}
//do other backend stuff here
?>
Using javascript for validation is only good in so far as it gives the user immediate feedback whether he did something wrong. However, a client can always see client side code like jQuery/Javascript, so for ultimate validation use php.

How to prevent a "space" from searching MySQL database?

Currently, when someone just hits the space key and hits enter it will go to the next page but not search anything - I want to prevent the search altogether. What is the best way to accomplish this? I don't want to prevent spaces from being used (ie: How do I fix this?) - I just dont want spaces themselves to allow a search.
Wrap your query variable in an empty condition:
if(!empty(trim($_POST['searchterm']))
{
// do search
}
Use JavaScript and trim leading spaces in the submit (onsubmit) event handler:
var searchField = document.getElementById('search'); // or whatever the id of the field is
if(searchField.value.replace(/^\s+/, '').length === 0) {
return false; // or evt.preventDefault().
}
It should be okay to rely on client-side validation here because if the user wants to fool the search engine then they won't mind being brought to a blank page. If there's an actual server-side problem in allowing this, then perform the same check server-side:
if(!isset($_REQUEST['search']) || !trim($_REQUEST['search'])) {
// Don't perform the search
}
In addition to #AlienWebguy answer you can use JavaScript to do client side validation in order to stop the page from even getting to the back end. Its definitely a good practice to do the validation on the client side AND server side.
Live Demo
form.onsubmit = function(){
if(input.value.replace(/^\s/,"").length === 0){
return false;
}
}
Just take the string, trim the initial and final spaces and check the length; if length is 0, don't submit the form.
If you are procecssing it with php on the backend you can just use trim($input), but for a better user experince use javascript. Set a form validator so it won't submit unless there is something other than whitespace.
<form onsubmit="return verify()">
<input id="foo" name="foo" />
<input type="submit" />c
</form>
<script type="text/javascript">
function verify() {
if (document.getElementById("foo").value.match(/[^\s]/) == null) {
alert('only whitespace');
return false;
} else {
alert('found substance');
return true;
}
}
</script>

Categories