I have developed following code. But while typing i need to remove < > these two charectres. Its removing but it removing entire string when we type in middle. I donr want to remove entire string i want remove only < > while typing.
Enter your name:
<input type="text" id="UserC" onkeyup="rem()">
function rem() {
var spclChars = "<>"; // specify special characters
var content = document.getElementById("UserC").value;
for (var i = 0; i < content.length; i++) {
if (spclChars.indexOf(content.charAt(i)) != -1) {
document.getElementById("UserC").value = "";
return false;
}
}
}
You can use regex for that:
var str = 'hello<name>'
function rem(string) {
return string.replace(/<|>/g, '')
}
console.log(rem(str))
this will output helloname.
Use the below code it's working perfectly...
$(document).on('keypress', "#inputid", function(e) {
var check_val = $("#inputid").val();
if ((e.which == 60 || e.which == 62)) { // < ascii value is 60 and > ascii value is 62
console.log(check_val);
// $(this).attr("placeholder", "digits only");
// $(this).addClass("alert-danger");
$(this).val(check_val);
return false;
} else {
$(this).removeClass("alert-danger");
}
});
Related
I'm trying to capture the character just entered into a <textarea>, but I can only get which key is pressed via key event like keydown or keyup, not knowing if it's lower case or upper case.
For example, when I input A or a, the event key codes for keydown are all 65.
I thought of using val() to get the string in the <textare> and get the last character of it, but that is too slow and memory consuming, since I want to record every keyboard event while the user is typing.
So is there a way I can simply get the last entered character?
Try this:
var p = $('#log')[0];
$("#textarea").on("keypress", function(e) {
p.textContent = '';
var k = e.keyCode || e.which;
var character = String.fromCharCode(k);
if (!isNaN(character * 1)) {
p.textContent += 'character is numeric';
} else {
if (character == character.toUpperCase()) {
p.textContent += 'UPPER case true';
}
if (character == character.toLowerCase()) {
p.textContent += 'lower case true';
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="textarea"></textarea>
<p id="log"></p>
I see what you mean about the shiftKey
var myObj = $('#myTextarea');
function isLetter(char){
if ((char.toLowerCase() !== char) || (char.toUpperCase() !== char)) {
return true;
}
return;
}
myObj.keypress(function( event ){
var text = myObj.val();
var char = text.charAt(text.length-1);
if (!event.shiftKey && isLetter(char)) {
if (char == char.toUpperCase()) {
console.log('Upper');
}
if (char == char.toLowerCase()) {
console.log('Lower');
}
}
});
try:
<textarea id="area1"></textarea>
window.onload = function () {
document.getElementById("area1").onkeypress = function(event){
var code = event.which;
if ((code >= 65) && (code <= 90)) {
alert('Upper');
}
else if ((code >= 97) && (code <= 122)) {
alert('Lower');
}
}
}
I am trying to make a typing game in javascript with jQuery but facing a issue.
How can I highlight the character the user types when they type it?
I have example in my div
<div id="theWord">tjurkey</div>
When the user starts typing "tj.." it should highlight t, then j, as they type it.
Currently I am stuck here:
$("body").keypress(function(e) {
if (e.which !== 0) {
var t = String.fromCharCode(e.which);
if ( t != undefined){ wordContainer += t.replace("undefined",""); }
if ( wordContainer == theWord){
alert("You typed the word" + theWord);
}
}
});
Ex. the word is "Tjurkey", if user start typing P it shouldn't highlight anything, because It's TJurkey and not P.
If user types "T" to start with it should highlight the "T" like Tjurkey, if user type "a" after that it shouldn't highlight it, because the word is Tjurkey and not Ta.... when the user then type j it should hightlight the j, because the word is TJ...urkey.. got the point?
Demo: http://jsfiddle.net/cVaHb/
var $target = $('#theWord'),
t = ''
$("body").keypress(function(e) {
if (e.which !== 0) {
t += String.fromCharCode(e.which);
var text = $target.text(),
pos = text.search(t);
if(pos > -1){
$target.html(
text.substring(0,pos)
+'<span class="highlight">'+t+'</span>'
+text.substring(pos+t.length)
);
}else{
$target.text(text);
}
}
});
CSS:
.highlight {
background: yellow;
}
Edit: If you want to ignore wrong letters, you can use
var $target = $('#theWord'),
t = ''
$("body").keypress(function(e) {
if (e.which !== 0) {
var newt = t + String.fromCharCode(e.which);
var text = $target.text(),
pos = text.search(newt);
if(pos > -1){
t = newt;
$target.html(text.substring(0,pos)+'<span class="highlight">'+t+'</span>'+text.substring(pos+t.length));
}
}
});
Demo: http://jsfiddle.net/cVaHb/1/
Here, to get you started
var t = "";
var word = $("#theWord").text();
$("body").keypress(function (e) {
if (e.which !== 0) {
t += String.fromCharCode(e.which);
if (word.substring(0, t.length) == t) {
$("#theWord").html("<span class='highlight'>" + t +"</span>"+ word.substring(t.length));
}
else
{
t=t.substring(0,t.length - 1);
}
}
});
check it out here:
http://jsfiddle.net/zahirdhada/UBbF7/1/
You can get the typed characters and find the starting and ending points of those in your string. Then you have to wrap that text with a span
ex: if user typed tj you should write a script to fill
<div id="theWord"><span style="color:red">tj</span>urkey</div>
I have a function for validating telephone and mobile numbers. Here is part of my function:
function IsPhone(){
var mob = /09[123]\d{8}$/;
var phn = /0\d{10}$/;
for (var i = 0; i < edit_rows.length; i++) {
if (edit_types[i] == 5) {
var phon_val = document.getElementById('phone1').value;
if (phon_val != "") {
if (phon_val.match(mob))
return true;
else if (phon_val.match(phn)) {
if ((phon_val).length == 11)
return true;
}
else {
msg_req += "Invalid format";
return false;
}
}
}
}
return true;
}
But it accepts all of these:
009153842716
09153842716
001234567890
01234567890
what can I do?
I think adding a ^ at the beginning of your expression would fix it. Your current query would match strings like 'thisisaninvalidvalue09153842716'. Adding the ^ makes sure you don't start with invalid input.
This is my first webpage in which I prompt the user for a phone number to add to a Do Not Call List database. Everything is working so far but I need to add the following, which I can do following the advice in this answer
stripping the input from all characters except digits
validating that the resulting string is 10 digits long
Then, when telling the user that the number was added to the list, I want to present it in the (999) 999-9999 format.
Where should I add all that code? Iside the #{ } block? In JavaScript? Razor?
Check phone number
function IsNumber(s) {
var i, currentCharacter;
for (i = 0; i < s.length; i++) {
// Check that current character is number.
currentCharacter = s.charAt(i);
if (((currentCharacter < "0") || (currentCharacter > "9"))) {
return false;
}
}
// All characters are numbers.
return true;
}
function TestInternationalPhone(strPhone) {
var bracket = 3,
openBracket,
phoneNumberOnly,
phoneNumberDelimiters = "()- ",
validWorldPhoneChars = phoneNumberDelimiters + "+",
minDigitsInIPhoneNumber = 10;
strPhone = SOS.StringHelper.Trim(strPhone);
if (strPhone.length === 0) {
return false;
}
if (strPhone.indexOf("+") > 1) {
return false;
}
if (strPhone.indexOf("-") != -1) {
bracket = bracket + 1;
}
if (strPhone.indexOf("(") != -1 && strPhone.indexOf("(") > bracket) {
return false;
}
openBracket = strPhone.indexOf("(");
if (strPhone.indexOf("(") != -1 && strPhone.charAt(openBracket + 2) != ")") {
return false;
}
if (strPhone.indexOf("(") == -1 && strPhone.indexOf(")") != -1) {
return false;
}
phoneNumberOnly = SOS.StringHelper.StripCharsInBag(strPhone, validWorldPhoneChars);
return (IsNumber(phoneNumberOnly) && phoneNumberOnly.length >= minDigitsInIPhoneNumber);
}
I have a form. I want to put validation so that It will check if user enters white spaces or not. If its white spaces then show error. How could I do this?
In case you want to detect if there is any white space all through the user's input string,
var str = $("input").val();
if( str.indexOf(" ") !== -1 )
{
alert("bad input");
}
Example: http://jsfiddle.net/pYquc/
Use jQuery.trim(str) which remove the whitespace or tabs and you can validate.
http://api.jquery.com/jQuery.trim/
Try this usong javascript:
var v = document.form.element.value;
var v1 = v.replace("","");
if( v1.length == 0){
alert("error");
}
OR you can use following functions:
// whitespace characters
var whitespace = " \t\n\r";
/****************************************************************/
// Check whether string s is empty.
function isEmpty(s)
{ return ((s == null) || (s.length == 0)) }
/****************************************************************/
function isWhitespace (s)
{
var i;
// Is s empty?
if (isEmpty(s)) return true;
// Search through string's characters one by one
// until we find a non-whitespace character.
// When we do, return false; if we don't, return true.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (whitespace.indexOf(c) == -1) return false;
}
// All characters are whitespace.
return true;
}
function doValidations(){
if(jQuery.trim( $(".className").val())==""){
alert("error message");
return false;
}else{
return true;
}
}
<input type="submit" onclick="return doValidations();">