Chinese character forbid - php

I have a web form, and I'd want the user while filling the form to not include any Chinese single quotes/double quotes in a textarea, how can I do that ?

You could get the form content and check it against a list of non accepted characters:
$formContent = $_GET["formcontent"];
$badCharacters = array("'", "‘", "“");
$cleanResult = str_replace($badCharacters, "", $formContent);
The bad characters array has the quote signs you dont like (if they are the standard quotes you will need to escape them) then the str_replace function goes through the form data replacing each one with an empty string. You should do proper escaping and security before this of course.

To prevent user from typing into form element you must use client side scripting aka JavaScript.
Simple way is handling the onkeypress event:
<textarea onkeypress="return CheckKey(event);"></textarea>​
And having such code:
var arrForbiddenCodes = [97, 98, 99];
function CheckKey(event) {
var keyCode = event.keyCode || event.which;
for (var i = 0; i < arrForbiddenCodes.length; i++) {
if (arrForbiddenCodes[i] === keyCode)
return false;
}
return true;
}​
This will block characters based on their key code - in the above code, the lower case "a", "b" and "c" letters will be blocked. To find key code of specific character, add alert(keyCode); to the function and just type that character into the textarea.
Live test case.

you could add some javascript that uses a regex to restrict the entry to alpha or alpha and numeric (or any additional characters you want).
<script language="javascript">
function blockChar()
{
var str = document.getElementById('txt').value;
if(str.match(^[a-zA-Z0-9]*$)) {
return true;
}
else {
return false;
}
}
</script>

Related

Replace special characters with alphabet in jQuery

How to remove Special Character and alphabet from variable by jQuery.
I have
var test = +985
But I want to get value as
var another = 985
It should not allow character as well + as I am using it in Phone Code
try to something like this...
var inputString = "~!##$%^&*()_+=`{}[]|\:;'<>,./?Some actual text to keep, maybe...",
var outputString = inputString.replace(/([~!##$%^&*()_+=`{}\[\]\|\\:;'<>,.\/? ])+/g, '-').replace(/^(-)+|(-)+$/g,'');
Might be duplicate of this
You just need to use replace function it's first parameter is symbol you want to replace and another is symbol or word you want to replace with.
For more info please visit Javascript replace function
var test = "+985";
var another = test.replace("+", "");
Try this to extract number:
var test = +985;
alert(test+"".match(/\d+/));

Force an input box to start with a character

I've searched quite a bit for this, but to no avail. I'm trying to make my input box start with a "#". It cannot just be a <span>#</span>, it needs to be in the input box itself. Is there no way of doing this?
You can also do this on the client side with a bit of javascript
function addHash(elem) {
var val = elem.value;
if(!val.match(/^#/)) {
elem.value = "#" + val;
}
}
and the HTML
<input type="text" onkeyup="addHash(this)"/>
Every time the user enters a character into the textbox, addHash would check if the first character is a hash, and if it isn't, then adds the hash mark.
If you want to check this server-side, since users can and probably will erase it:
function prependHash($string) {
if(substr($string, 0, 1) === "#") { return $string }
else return "#" . $string;
}
Relevant HTML:
<input type="text" name="someInput" value="#">
Relevant PHP:
//just in case they erase the hash:
$hashedValue = prependHash($_POST['someInput']);

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
}

jQuery replace function (trigger needed)

i got a jQuery function to check a textarea for "valid" urls while someone is typing.
$(".url2").keyup(validNum).blur(validNum);
function validNum() {
var initVal = $(this).val();
outputVal = initVal.replace(/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])$/,"replace this link by bla..");
if (initVal != outputVal) {
$(this).val(outputVal);
}}
});
problem is, that the functions already replaces the url as soon as "http://www.ab" is typed instead of waiting till space is pressed (complete url, waitig for space, replace with this function). I want to achieve, that people can type in a whole url like http://www.example.org/site?id=1&etcetc before it gets replaced. So I think of "space" as a trigger to start the function. Can someone help me or someone got a better idea?
Thank you so much
wordi
If you want space or whitespace chars to trigger validation, use this:
$(".url2").keyup(validateOnWhiteSpace).blur(validNum);
function validateOnWhiteSpace() {
if(event.keyCode == 9 || event.keyCode == 13 || event.keyCode == 32) {
validNum.call(this, arguments);
}
}
function validNum() {
var initVal = $(this).val();
outputVal = initVal.replace(/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])$/,"replace this link by bla..");
if (initVal != outputVal) {
$(this).val(outputVal);
}}
});
I think if you just change your regex you will get what you want
/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])(\s){1}$/
Here is a jsfiddle that seems to work
I can't yet comment on the first thread, but to answer your question about the PHP regexp not working in jQuery, it is because you have ^ at the beginning and $ at the end. The ^ matches the beginning of the string and $ matches the end of the string. Taking it out will allow the regexp to match at any point in the string.
Example:
^abc\d$
Will match:
"abc1"
Will NOT match:
"Zabc1"
Since the "a" is not the first character of the string.
$(".url2").onchange(validNum).blur(validNum);
function validNum() {
var initVal = $(this).val();
outputVal = initVal.replace(/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])$/,"replace this link by bla..");
if (initVal != outputVal) {
$(this).val(outputVal);
}
}

Compare array of words to a textarea input with javascript

I have a textarea inside a form.
Before the form is submitted, the textarea is validated and checked so it is not empty, not over 2000 characters, not contain forbidden characters etc...
I am working on the last part of the validation, which would need the textarea to be compared to an array of "bad words".
This to help me maintain and keep a good "language" on my site.
I am not very good on js, so does anybody know of a way to compare each word of the textarea to the array of bad words?
Also, would this slow down the validation much? (the array contains at most 100 words).
Thanks
If you wanted to check for the presence of "expletive1" and "expletive2" you'd do the following:
my_textarea = document.getElementById('textarea_id');
if (/\b(?=\w)(expletive1|expletive2)\b(?!\w)/i.test(my_textarea.value)) {
// we found bad words! do something
} else {
// no bad words found, carry on, nothing to see here
}
And you'd just add more words to the list in the same manner (expletive1|expletive2|expletive3|expletive4)
Keep in mind that to keep the words out of your app entirely you'll also need to do server-side filtering.
var bad_words = ['stupid', 'dang']; // watered down
for (var i = 0; i <= bad_words.length; i++) {
if (document.getElementById('my_textarea').value.match(bad_words[i])) {
// has bad word!
}
}
This will keep your code a bit neater, because you don't have to have 100 words in one regex match.
This code replaces bad words with *****
// creating regex
var words = ['bad', 'words'];
var wordsStr = "";
for(var i=0; i<words.length; i++) {
wordsStr += words[i];
if (i < words.length -1) {
wordsStr += "|";
}
}
// wordsStr is "bad|words"
var regex = new RegExp(wordsStr, "gi"); // g: replace all; i:insensitive
// replacing
var text = "I cant say bad words!";
text = text.replace(regex, "****");
// text is "I cant say **** ****!"
See in jsfiddle
var bad_words = new Array('word1', 'word2');
var user_words = document.getElementById('textarea').split(/\W+/);
for( var i in bad_words)
{
if( user_words.indexOf( bad_words[i] ) != -1 )
{
alert( 'The textarea has bad word!');
break;
}
}
You can downvote me if you want, but maybe just don't make the clbuttic mistake of trying to filter in the first place :D

Categories