So I'm using Knack and a Jquery code snippet to extend the Knack featureset. I want to validate a telephone number entered into a form on Knack. Knack provide this code example, but it doesn't do quite what I want it to do.
// Your app's ViewID and FieldID will need updated.
$(document).on('knack-view-render.view_97', function(event, view, data) {
$("#view_97 .kn-submit input[type=submit]").on("click", function() {
// if this value in my form doesn't equal "SpecificValue" then prevent the form from submitting
if ($("#view_2-field_29").val() != "SpecificValue") {
alert ("These are not the droids you are looking for.");
return false;
}
})
})
Basically I would like the validation to either strip out (preferred) or not accept (next best!) spaces and insist on being given a mobile number which is either " " (a space) or a number in the format 00000000000. 11 digits.
Thank you for any help!
To strip out any non-digits in the number and take the first 11:
$("#view_2-field_29").val().replace(/\D/g,"").slice(0,11)
Related
I have a dynamic grid that has a numeric field in which I have applied masking. Also it has a plus button means the user can add many rows as he wants. The masking is applied like this:
<input type='textbox' placeholder='00000-00-000' data-mask='00000-00-000'
This masking is applied when the user enters 10 digit number but it lets the user enter a 2 or 3 digit number as well. For this I am trying to apply validation while saving so that it checks whether the value entered matches the required format.
What I have done so far is :
value = $(this).find('td:eq(1)').find('input').val(); //saves the entered value in a variable value
myRegExp = new RegExp(/\d{5}-\d{2}-\d{3}/);
if (!myRegExp.test(value)) {
valid = false;
}
else
valid = true;
The value that user enters is saved in varaible called value and then I have defined my Regex Expression to match my value with that expression but somehow this regex expression is not working. Is this the correct way to define regex expression? What am I missing here? My required format is 00000-00-000. Any help would be appreciated.
Your logic is correct but you have not defined the end point that's why it allows to insert more values.
In your Regex it only checks if the 10 digits are in the specific order
try out this
myRegExp = new RegExp(/^\d{5}-\d{2}-\d{3}$/);
I currently have a textbox that can take one or multiple (comma separated) values.
var textBox = $('#testValues').val().split(",");
I'm trying to add regex validation that when users enter one or multiple values (always has to be 12 hex characters) and hit submit, then the regex checks and makes an API call if it passes else throws an alert message.
I currently have like this.
var regexValidation = new RegExp("^[a-zA-Z0-9]{0,12}$");
if (regexValidation.test(textBox)) { apiCall }
else { alert('Regex validation failed'); return false;}
It works great when I ONLY entered 1 value. However, if I try to enter multiple values with "," then it just throws an alert message. I do not want to add a comma in the regex because then it will take 12 hex characters including a comma. How should I handle it?
Thank you.
Just add multiple optional values:
var regexValidation = new RegExp("^\s*[a-zA-Z0-9]{12}(?:\s*,\s*[a-zA-Z0-9]{12})*\s*$");
Developing a PDF form that requires field validation. I am using Bluebeam however from what I see so far it works exactly the same as any other PDF form creator.
So I am trying to validate the field for number characters only and accept the value if it is number characters (must also accept text starting with single and multiple 0's). The following was having a guess of what it might have to use.
var fld = this.getfield(“Text1”)’
If IsNumeric(“Text1”) == true;
event.value = fld.value
else
alert(“Error message”)
Try this hope this helps:
var fld = this.getfield(“Text1”).value;
if(fld.match(/^\d+$/)){
event.value = fld.value;}
else{
alert(“Error message”);}
I would like to build a form input using the mask plug-in that will change the characteristic of the entry for time to permit the user to enter digits in a free form format and the system will convert the entered digits into proper time display using jQuery.
Example: the user to enter “135” and the system to convert this to “01:35”
The code for the mask is:
$("input.time").mask("99:99");
So far I have the code below:
$(document).ready(function() {
$('#awareness-hour').blur(function() {
if(!$('#awareness-hour').val().match(/([0-1][0-9]|2[0-3]):[0-5][0-9]/)){
alert('invalid');
}else{
return true;
};
});
});
This will correct the scenario you mentioned and update the input only if it's a valid time according to your second regular expression. Hopefully, you get the idea and this works:
$("input.time").blur(function() {
var userInput = $(this).val().replace(/^([0-9])([0-9]):([0-9])_$/, "0$1:$2$3");
if(!userInput.match(/([0-1][0-9]|2[0-3]):[0-5][0-9]/)){
alert('invalid');
}else{
$(this).val(userInput);
return true;
};
}).mask("99:99");
I have a text box that is going to be validated in JavaScript upon click on the submit button.
Only the character 0-9 and a-f and A-F are allowed.
So g-z and G-Z as well as other characters such as punctuation or not allowed.
The code I have so far is:
function validate_form ( )
{
valid = true;
if ( document.form.input.value == [a-zA-Z_,.:\|] )
{
alert ( "You can only enter either 0-9 or A-F. Please try again." );
valid = false;
}
return valid;
}
Which doesn't seem to work.
I'm new to JavaScript so can any one please give me a hint as to where I'm going wrong?
We can actually clean this code up a lot. There's no need to keep track of valid as test() will provide us with the true or false value we're looking for. It's also a good deal easier in your case to keep a whitelist of acceptable characters rather than a blacklist. That is, we know every character we want, but can't possibly specify every character we don't want.
function validate_form() {
return /^[a-fA-F0-9]+$/.test(document.form.input.value);
}
Note that you can also use this to do a pre-check:
document.form.input.onkeyup = function() {
if (!validate_form()) {
alert("You can only enter either 0-9 or A-F. Please try again.");
}
};
the syntax is /^[a-zA-Z_,.:\|]+$/.test(document.form.input.value). Notice the ^ and $: without them, the test will pass even for strings that have only at least one allowed character.
The best way for validation is to not let the user, enter wrong character. Use this code (this is the jQuery version, but you can also convert it easily to JavaScript):
$('#inputFiledId').keyup(function(e){
// checking the e.keyCode here, if it's not acceptable, the return false (which prevents the character from being entered into the input box), otherwise do nothing here.
});
This is called pre-check. Please consider that you whatever you do in client-side, you should always check the values at the server also (have server-side validation) as there are multiple hacks around, like form-spoofing.
You could do something like this
$('input').keyup(function(){
var charac = /[g-zG-Z;:,.|_]/;
var result = charac.test($(this).val());
if(result == true){
alert('You can only enter either 0-9 or A-F. Please try again.');
}
})
http://jsfiddle.net/jasongennaro/GTQPv/1/