block this special characters using query `~ #$^+|\ []{}<> - javascript

I want to block some special characters from entering in text box.
`~ #$^+|\ []{}<> these are the characters i would like to prevent from user inputting. Can any one suggest a solution or reg ex.

function some(){
var input = document.getElementById('one').value;
var one = input.replace(/[(\\^`~#\$\^\+\|\\\[\]{}<>)]/g,"");
document.getElementById('one').value=one;
}
<input type="text" oninput="some()" id="one" >

<input type="text" pattern="[^`~#\$\^\+\|\\\[\]{}<>]+">

Related

Check if string that must be just numbers has characters

I have a input for phone number, and its type is not number is text, and I want to check if the input has characters to validate it as wrong, I have set it like that because I put a format to the input like 123-123-1234
here is my input
<input (keyup)="format()" (change)="format()" maxlength="12" inputmode="numeric" type='text' class="input" formControlName="celular" id="celular" name="celular">
Here is my ts where I set the format
format(){
$('#celular').val($('#celular').val().replace(/^(\d{3})(\d{3})(\d+)$/, "$1-$2-$3"));
}
so what I want to do, is to know if the value of my input has characters from aA-zZ and some specials characters that are not -
With a little help from google i found this:
Regex phoneRegex =
new Regex(#"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");
if (phoneRegex.IsMatch(subjectString)) {
string formattedPhoneNumber =
phoneRegex.Replace(subjectString, "($1) $2-$3");
} else {
// Invalid phone number
}
You can use html regex pattern for validating input fields
Example:
Format used in this example is 123-123-1234
<form>
<input type="text" placeholder="Enter Phone Number in format (123-123-1234)" pattern="^\d{3}[-]\d{3}[-]\d{4}$" title="Invalid input" size="50" />
<button type="submit">Submit</button>
</form>
I have found the solution
var b = /[a-z A-Z]/
var a = '123a'
b.test(a)
this gives true or false depending if the var a has any character from a-z and A-Z

Html Text box only allow numeric while typing thousand separator and decimal

Hi i want to allow html text box accept numeric value only and while typing it should transform like thousand separator and two decimal
Example :
12,343.50
Please try this the input text field will not allow any characters except numbers and value of input field will be like Indian Currency.
$("#formattedNumberField").on('keyup', function(){
var n = parseInt($(this).val().replace(/\D/g,''),10);
$(this).val(n.toLocaleString("en-IN", { currency: "INR"}));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="formattedNumberField" value="" />
And Please refer the above Code Snippet Link.
Thanks.
Here I have wrote a javascript function. You can use this to convert to commas.
HTML
<input type="text" name="x1" onkeydown="numberWithCommas(this)"/>
Javascript
function numberWithCommas(x) {
setTimeout(function(){
if(x.value.lastIndexOf(".")!=x.value.length-1){
var a = x.value.replace(",","");
var nf = new Intl.NumberFormat();
console.log(nf.format(a));
x.value = nf.format(a);
}
},1);
}

javascript validation for home address. Ex: #55 Bernahrd Gym

I need a validation for address that can have Numbers, alphabets, space in two words should be able .. but no space in the beginning of the address.
The only symbol should be #
EX: #55 Bernahrd Gym
Address:<input type="text" required="required" name="baguio_add"><br>
You can use regular expressions for this: /[a-z0-9\s\#]+/i
Instead of testing for spaces at the start, just use trim (or ltrim).
Edit: You changed your tag to Javascript, you can still use regular expressions for this using the regex above.
You can use this regex if you want only one #, and any number of alpha numeric plus spaces.
/#?[a-zA-Z\s\d]+/
If it always starts with # then:
/^(#?)[a-zA-Z\s\d]+$/
Here is how you use it:
HTML:
<input name="address" id="address" type="text" />
Javascript:
document.getElementById('address').onblur = function (e) {
console.log(this.value);
var isValid = /^(#?)[a-zA-Z\s\d]+$/.exec(this.value);
if (isValid){
// do any action on valid
this.className = 'valid';
} else {
this.className = 'invalid';
}
}
Here is a working example in jsfiddle: https://jsfiddle.net/uua6pp1q/

Cannot get JavaScript password regex validation to work

I am trying to validate a user entered password. It can only be letters and numbers and I have to use the match method...even if it isn't the best way...I have to use match(). I am missing something to get the working properly. No number or special characters only letters. I do not know much about javascript.
<script type="text/JavaScript">
function chkPwd() {
var pwd = document.form1.pwd;
var pwdMsg = document.getElementById('pwdMsg');
regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(!pwd1.match(regex)) {
pwdMsg.innerHTML = "Must contain letters only!"
pwd.select();
return;
}else{
pwdMsg.innerHTML = "";
}
}
</script>
</head>
<body>
<form name="form1" action="" method="post">
<p> Password: <br>
<input type="text" name="pwd" onchange="chkPwd()" />
<span id="pwdMsg"></span></p>
<p>
<input type="button" value="chkPass" onclick="chkPwd()">
</p>
</form>
<div id="results"></div>
</body>
</html>
Your issue appears to be in this section of code.
regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(!pwd1.value.match(regx)) {
You are setting the pwd1 to pwd.value, but then on the next line, you are accessing pwd1.value. This means that you are efficiently doing pwd.value.value. Additionally, you are using regx where you should use regex. Also, your if condition does not appear to need a ! in it. I think you mean to do this.
regex = /[^a-zA-Z]/;
var pwd1 = pwd.value;
if(pwd1.match(regex)) {
If you want letters only you should use this regex:
^[A-Za-z]+$
If you want to have letters and numbers then use:
^[A-Za-z0-9]+$
Btw, as guys pointed out you are using regx instead of regex

user can enter in text box just numbers and this signs (, .)

I want to restrict user to enter in text box just numbers and this signs (, .) when the user try to enter letters or other signs textbox can't accept them How can I do that with jquery?
<input type="text" pattern="[0-9,.]+" title="Please enter only numbers" />
No jQuery needed. Demo
If you're actually looking for an optionally thousand-separated decimal number, try:
pattern="(?:[1-9][0-9]{0,2}(?:,[0-9]{3})+|[1-9][0-9]*)(?:\.[0-9]+)?"
Advanced demo
Be sure to do the same validation server-side!
jQuery not needed.
HTML:
<input type="text" id="ID">
JS:
document.getElementById("ID").onkeyup = function() {
var text_content = document.getElementById("ID").value;
if(/[^\-,0-9]/.test(text_content)) {
text_content = text_content.substring(0, text_content.length - 1)
document.getElementById("ID").value = text_content;
}
}
Fiddle. Hope that was what you were looking for.

Categories

Resources