I have a input type URL and client want to only accept ENGLISH characters on input
Keypress and Blur function is what I have but in blur it still accept this likr inout "建築家test.com"
<input type="url" id="url" required pattern="https?://.+" placeholder="http://example.com">
$("#url").on("keypress", function(event) {
var englishAlphabetDigitsAndWhiteSpace = /[a-zA-Z0-9!##$%^*_|:/.]/g;
var key = String.fromCharCode(event.which);
if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetDigitsAndWhiteSpace.test(key)) {
return true;
}
return false;
});
$("#url").blur(function(e){
var input = $(this).val();
var regex = /[a-zA-Z0-9!##$%^*_|:/.]/g;
if(regex.test(input)) {
alert("OK");
}
else {
alert("no Japanese allowed");
return false;
}
});
Could you try with the following regex:
^([a-zA-Z0-9!##$%^*_|:/.])*$
or that one, depending on if you accept empty string or not as valid entry.
^([a-zA-Z0-9!##$%^*_|:/.])+$
Last but not least, if you want to check that your input is a well-formed UR, have a look at the following link:
What is the best regular expression to check if a string is a valid URL?
Related
I am trying to restrict alphabets in javascript. Here is the code I am using and I am able to show alert to users "Enter numbers only" if they enter alphabets.
But issue is after entering values in the textbox, if I try to backspace the values, it shows alert("Enter numbers only"
How to not show alert if I press backspace key.
Here is my code,
PasswordCheck(event)
{
var allowedRegex = /^[0-9]+$/ && event.keyCode === 8;
if (!event.key.match(allowedRegex)) {
alert("Enter Numbers only")
event.preventDefault();
}
<form (keyup)="PasscodeCheck($event)" autocomplete="off">
......
</form>
PasswordCheck(event) {
var allowedRegex = /^[0-9]+$/;
if (!event.key.match(allowedRegex) && event.keyCode !== 8) {
alert("Enter Numbers only");
}
}
I tried the piece of code to restrict user if the address is wrongly entered as shown below
sample address: #23 78/54, A-31 Nand Jyot Indl Estate, Andheri- Kurla Road, Saki Naka (allowed)
14/18, Loha Bhavan, P D Mello Rd, Chinch Bunder, mumbai (allowed)
only ".....", ",,,,", "----" (not allowed)
$("#" + (idOfElement)).on("keypress keyup", function (e) {
this.value = this.value
//.replace(/[^\..]/g,'')
.replace(/(^[\d#\w]+)/g, '$1')
.replace(/(\s[1-9A-Za-z\/#-]*)./g, '$1');
});
I want the user allowed to enter numbers, '#', '/', '-', space, and alphabets only, and if they enter any other special characters it shouldn't allow them to do so.
code given below is working fine for number with decimal upto two position. similarly i want to do the same for address.
below code allows 12345.67
it doesn't allow 23.4565, 23289.499999
//for numbers with decimal point upto two position
$("#" + (idOfElement)).on("keypress keyup blur", function (e) {
this.value = this.value
.replace(/[^\d.]/g, '')
.replace(/(^[\d]+)([\d]*)/g, '$1')
.replace(/(\..*)\./g, '$1')
.replace(/(\.[\d]{2})./g, '$1');
if ((e.which != 46 || $(this).val().indexOf('.') != -1) && (e.which < 48 || e.which > 57)) {
e.preventDefault();
}
I also want the user to enter only alphabets with space and numbers only.
sample data: toothpaste 200g (allowed)
shampoo..(not allowed)
shampoo (with any other special character like !,?,_,~)(not allowed)
Use below code sample. Also please find fiddle for same in comment.
<input type="text" id="addressID">
$(document).ready(function(){
$("input").bind('input', function(e) {
var str = $("#addressID").val();
var re = new RegExp(/^[ A-Za-z0-9\/#-]*$/g);
if (re.test(str)) {
//alert('valid');
} else {
alert("Invalid address, only alphanumeric values and '/','#','-' allowed");
$("#addressID").val($("#addressID").val().slice(0, -1));
}
});
});
Do Regular expression have power to live format
entering string in this format ( /^\d{3}\-?\d{1,13}\-\d{2}$/ ). By that I mean , when I type ( 123 ) , he automatic put (123-).
I use jQuery function which I listed it in the example below.
--Point is when user what to type his bank account I want to live format it.
---Can this job be finished by Regexp ---
HTML:
<input type="text" id="radi" />
JavaScript:
//This work on every typed character
$("#radi").bind('input', function(event) {
$("#radi").bankPlug('change', $(this).val());
});
You can use the keypress function followed by the replace function. replace can take a function as a second parameter. Here's one possible way to do it.
$("input").on("keypress", function(e) {
var inputNum = $(this).val();
if (e.keyCode != 45 && (e.keyCode < 48 || e.keyCode > 57)) {
return false;
}
$(this).val($(this).val().replace(/\d{3}/, function(n) {
return (/\d{3}-/).test(inputNum) ? n : n + "-"
}));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="number" />
I have one amount field in that i want to restrict user after decimal they can not use more than two values:
After decimal there should be only two values that are option.
User can not type more than one decimal in the field.
I am using following regix for that its not working can any one help.
function validate(evt) {
if (evt.keyCode == 8) { return true; }// for backspace problem in mozilla
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
var regex = /[0-9 ]|\,\d{1,2}/;
if (!regex.test(key))
{
theEvent.returnValue = false;
if (theEvent.preventDefault) theEvent.preventDefault();
}
}
This can actually be accomplished using HTML only by setting the STEP and MAX of your numeric field such as
<form>
<input type="number" name="myNumber" required=true step=".01">
<button>send</button>
</form>
please let me know if this is what your looking for but this will mean that any value entered must be numeric(thus only 1 decimal point) it must be less than or equal to 1 (you can set to .99 if you dont want to include 1) and it must be an increment of .01(so it can only have 2 decimal places.
Just re-read your question you can remove the max(i thought i read no more than 1.0)
var previousValue;
function numberChanged(element){
if(validateMe(element)==false){
element.value=previousValue;
}else{
previousValue=element.value;
}
function validateMe(element){
wholeNumber=element.value.split(".")[0];
if(element.value.split(".").length==1){
if(wholeNumber<-1 || > 999999999){
return false;
}
}else if(element.value.split(".").length==2){
decimalValue=element.value.split(".")[1];
if(decimalValue>-1 && decimalValue<100){
if(wholeNumber<-1 || > 999999999){
return false;
}
}else{
return false;
}
}else{
return false;
}
}
<input type="text" id="myNumber" onChange="numberChanged(this)" />
For an input field I have used a class
Here code for input field:
<?php
echo $this->Form->input('duration', array('class'=>'input-large text-right number- field','value'=>'0'));
echo $this->Form->error('duration');
?>
Here the jquery class code:
$(".number-field").keypress(function(e) {
var code = e.which || e.keyCode;
if(code >= 48 && code <= 57) {
console.debug(code);
} else {
return false;
}
});
Main reason In input field use can't type character in digits field. This class working fine.But after type digits backspace not working. For example suppose I have typed 1956, now I want to edit it 1955. In here I am enable to cut 6 by backspace.
You can use following script -
$(".number-field").keypress(function(e) {
var code = e.which || e.keyCode;
//allow Backspace and Delete key.
if((code >= 48 && code <= 57) || code == 8 || code == 46) {
console.debug(code);
} else {
return false;
}
});
Similar to escaping the 'backspace' key you might want to escape the 'Delete' key(code == 46)
If you just want to allow the numbers in input type then you can use HTML5 input type number.
<input type="number" name="year">
You can use regex for this:
var reg = /^\d+$/; // only number
$(".number-field").keypress(function(e) {
var code = e.which || e.keyCode;
if(reg.test(String.fromCharCode(code))) {
console.debug(code);
} else {
return false;
}
});