Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have one form with textbox and user needs to insert width X height format value so I need to validate whether it is in correct format or not using JavaScript regexp validations.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<input type="text" name="txt" class="txt_val" value=""/>
<input type="submit" class="clk" value="submit"/>
<script>
$(".clk").click(function(){
var number=$(".txt_val").val();
var compare=/[0-9]{2}^[a-z .x-]{1}[0-9]{2}/;
if(number.match(compare)){
alert('match');
}
else{
alert('not match');
}
});
</script>
Expected output with cases:
1) format should be like this
10X10 or 23X24 or 36X24 or 44X56
like any width by height with uppercase or lowercase 'x'
2) any number with two digits following widthXheight and charecter 'x' can be upper case or lower case
Your regex should be /[0-9]{2}[x|X][0-9]{2}$/
Where:
[0-9] - Matches number in the range between 0 (index 48) and 9 (index 57)
{2} - Quantifier — Matches exactly 2 times
[x|X] - Match the character x or X
$ - Asserts position at the end of a line
$(".clk").click(function(){
var number=$(".txt_val").val();
var compare=/[0-9]{2}[x|X][0-9]{2}$/;
if(number.match(compare)){
alert('match');
}
else{
alert('not match');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<input type="text" name="txt" class="txt_val" value=""/>
<input type="button" class="clk" value="submit"/>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
It should not be possible to enter a sequence of numbers with more than 7 digits, these having spaces or not between the characters.
Example number sequences to block:
99999999999
99 9 9999 9999
99 9999 9999
<input type="text" name="texto" id="texto"/>
$('input:text, textarea').keyup(function(){
var num = $(this).val();
var a1 = num.split(" ");
for (i=0;i<a1.length;i++)
{
var total_letras = a1[i].length;
if($.isNumeric(a1[i])){
if(total_letras > 7)
{
$(this).val( num.substring(0, num.length - 2) );
$(this).addClass("input_bloqueado");
}
else
{
$(this).removeClass("input_bloqueado");
}
}
}
});
That is, if the user enters a sequence of numbers longer than 7 digits, the script must obfuscate or remove these numbers.
You can try this regex /((?:[1-9]\s*){7})/g or /((?:\d\s*){7})/g if you want the number from 0-9.
Demo:
$('#texto').keyup(function(){
var reg = /((?:[1-9]\s*){7})/g;
var texto = $(this).val();
var result = texto.replace(reg,"********") ;
$("#texto").val( result );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="texto" id="texto"/>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
In my project, there is a text box to enter the password and the password characters limit is up to 15. So, I want to display an alert message through javascript as soon as the user tries to enter the 16th character. Please help!
Try this.
function alrtMsg() {
var x = document.getElementById("pwd").value;
if(x.length > 16){
alert("maximum length is 16");
document.getElementById("pwd").value = '';
}
}
<html>
<body>
<input type="password" id="pwd" onkeyup="alrtMsg()">
</body>
</html>
Try this on Javascript:
document.getElementById("password").onkeyup = function() {
var text = document.getElementById("password").value
if(text.length> 15){
alert("too much text")
}
};
<input id="password" type="password" placeholder="password">
you need to mention your code what you tied. Anyway if you don't know try this code
<input type="text" onkeypress="myFunction(this)" maxlength="5">
<script>
function myFunction(e) {
var maxlen= e.maxLength;
if(e.value.length >= maxlen)
alert("Max length is limited to" +maxlen );
}
</script>
Using JQuery:
$("#inputFieldId").on("input propertychange", function(){
var val = this.value;
if(val.length > 15) {
this.value = val.substring(0,15); //comment if you don't want to remove the 16th character
alert("Maximum 15 characters allowed!");
}
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have full dialpad code .
Clear button clear all text from dialpad window. but i want to delete only last digit which i dial from dialpad.
language only javascript,jquery and html.
function del(){
var num=document.getElementById('num').value;
num = num.substr(0, num.length - 1);
document.getElementById('num').value=num;
}
function isValid(a){
if(!(/^[0-9]+$/.test(a.value))){
a.focus();
console.clear();
console.log("Please enter number");
}
}
<input type="tel" onblur="isValid(this);" id="num" /><button onclick="del();">Del</button>
jQuery - as per requested
$(window).load(function(){
$('button').on('click',function(){
$('#num').val($('#num').val().substr(0,$('#num').val().length-1));
});
});
//optional so not converted to jQuery
function isValid(a){
if(!(/^[0-9]+$/.test(a.value))){
a.focus();
console.clear();
console.log("Please enter number");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="tel" onblur="isValid(this);" id="num" /><button>Del</button>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here is the structure and data of my price
if number is between 0 and 2000 (or in other word the number is less then 2000) - then must appear tax "20"
if number is between 2001 and 5000 - then must appear tax 35
Price:<input type=text name=price><br>
Tax: <input type=text name=tax>
First you should add labels for your inputs, they are linked to the input with ids
<label for="priceInput">Price:</label><input id="priceInput" type=text name=price><br>
<label for="taxInput">Tax:</label><input id="taxInput" type=text name=tax>
And in your jquery:
//Function is executed when the priceInput is changed, so when you enter a value
$("#priceInput").change(function(){
//Test if the value of the input is < 2000 and >0
if (($( "#priceInput" ).val()<2000) && ($("#priceInput").val()>0)) {
//Set the value of the tax input to 20
$("#taxInput").val(20)
}
//test if tyhe value is >2000 and <5000
else if(($( "#priceInput" ).val()<5000) && ($("#priceInput").val()>2000)){
$("#taxInput").val(35)
}
else{
$("#taxInput").val("")
}
});
JSFiddle link
function setPrice()
{
if ($('[name=price]').val() < 2000)
{
$('[name=tax]').val(20);
}
else
{
$('[name=tax]').val(35);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I use a regular expression to validate the latitude and longitude for these text fields and display an error message?
<div class="form_style">
<fieldset>
<label for="contentText" class="fixedwidth">Name:</label>
<input type="text" name="content_txt" id="contentText" class="inputtext" ><br/>
</fieldset>
<fieldset>
<label for="address" class="fixedwidth">Address:</label>
<input type="text" id="address"class="inputtext" /><br/>
</fieldset>
<fieldset>
<label for="lat" class="fixedwidth">Lat:</label>
<input type="text" id="lat"class="inputtext" /><br/>
</fieldset>
<fieldset>
<label for="lng" class="fixedwidth">Lng:</label>
<input type="text" name="lng" id="lng"class="inputtext" /><br/>
</fieldset>
<fieldset>
<label for="type" class="fixedwidth">Type:</label>
<input type="text" id="type"class="inputtext" /><br/>
</fieldset>
<button id="FormSubmit">Add Marker</button>
</div>
<script type="text/javascript">
var latitude = document.getElementById(lat).value;
var longitude = document.getElementById(lng).value;
var reg = new RegExp("^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}");
if( reg.exec(latitude) ) {
//do nothing
} else {
//error
}
if( reg.exec(longitude) ) {
//do nothing
} else {
//error
}
</script>
Feel free to test your regex here:
^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}
Debuggex Demo
In this case, the regex would match a number that is negative or positive, either (1 digit excluding '0' and a '0') or (one or two digits, the first one excluding 9, both excluding '0'), followed by a decimal point and up to 6 other digits. If that's what you need, then yes, this would work. If you need a different format, post it in a comment and I'll try to work a proper regex.
Googling "regex" should give you all the info you need.
If you just need numbers between -90 and 90 (or -180 and 180) you can just do this:
if (typeof num === 'number' && num <= 90 && num >= -90)
//valid
else
//invalid
If you need symbols like ° (degrees) or compass direction, then regex could be beneficial.
Here is a review of a few formats:
http://www.geomidpoint.com/latlon.html
Just do this:
function isLatitude(lat) {
return isFinite(lat) && Math.abs(lat) <= 90;
}
function isLongitude(lng) {
return isFinite(lng) && Math.abs(lng) <= 180;
}
I used two different regexes for each value.
Latiude Regex:
const latRegex = /^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,15}/g;
Longitude Regex:
const lngRegex = /^-?(([-+]?)([\d]{1,3})((\.)(\d+))?)/g;