In this code:
this.getField("myField").value == null;
this.getField("myField").value === null;
typeof this.getField("myField").value == null;
typeof this.getField("myField").value === null;
this.getField("myField").rawValue === null;
this.getField("myField").formattedValue === "";
this.getField("myField").isNull;
this.getField("myField").isNull == True;
all of the above exchanging 'null' for 'Null', encapsulated 'Null', and 'undefined'.
In each circumstance all I get is:
TypeError: this.getField(...) is null
23:Field:Blur
How do I see if a field is null? I do not want to have default values because not every field on the form needs to be used and should be able to be blank.
If you're getting that error, it's because this.getField("myField") itself is returning null. So any attempt to use a property on what it returns will fail.
It sounds like you need a null guard:
var field = this.getField("myField");
if (field !== null) }
// use `field.value` here...
}
Generally... meaning you used an application rather than a library to create the form... PDF field values will never be null. An empty field has a zero-length string as the default value. To test if the field is empty use...
if (this.getField("myField").value.length == 0) {
// do something
}
else {
// it has a value
}
or
if (this.getField("myField").value == "") {
// do something
}
else {
// it has a value
}
Related
I'm populating a table with data - using fixed-data-table, which is a React.js component. However, that isn't so important at this stage.
The table has a search box where the issue stems from.
First, here's the interesting part of the code.
for (var index = 0; index < size; index++) {
if (!filterBy || filterBy == undefined) {
filteredIndexes.push(index);
}
else {
var backendInfo = this._dataList[index];
var userListMap = hostInfo.userList;
var userListArr = Object.values(userListMap);
function checkUsers(){
for (var key in userListArr) {
if (userListArr.hasOwnProperty(key) && userListArr[key].text.toLowerCase().indexOf(filterBy) !== -1) {
return true;
}
}
return false;
}
if (backendInfo.firstName.indexOf(filterBy) !== -1 || backendInfo.lastName.toLowerCase().indexOf(filterBy) !== -1 || backendInfo.countryOrigin.toLowerCase().indexOf(filterBy) !== -1
|| backendInfo.userListMap.indexOf(filterBy) !== -1) {
filteredIndexes.push(index);
}
}
}
This is rendered and the last part is throwing errors if you input something in the table, and a column returns null from the user input.
The thing is, I can make the code work if I change the last part to ..
try {
if (backendInfo.firstName.indexOf(filterBy) !== -1 || backendInfo.lastName.toLowerCase().indexOf(filterBy) !== -1 || backendInfo.countryOrigin.toLowerCase().indexOf(filterBy) !== -1
|| backendInfo.userListMap.indexOf(filterBy) !== -1) {
filteredIndexes.push(index);
}
}
catch(err) {
console.log('Exception')
}
With the try/catch, it works 100% as intended and handles the indexOf returning null... But this can't be the way to properly handle it - I'm assuming this sort of exception handling is, well, supposed to be for rare exceptions, and shouldn't really be used on the front-end as much as the backend.
How do I handle indexOf returning null in the above Javascript code? It might return null in any of the sources columns that are being populated.
If a key cannot be found, JS will throw an error. Try-catch is a good way to fix these errors, but there is an alternative:
You could check if keys exist in an object prior to pushing a value into it.
var data = { };
var key = "test";
// your method works great
try {
var value = data.firstname.indexOf(key);
} catch (err) {}
// another method, I'd prefer the try/catch
var value = data.firstname ? data.firstname.indexOf(key) : undefined;
// test if the object is the type of object you are looking for
// this is in my opinion the best option.
if(data.firstname instanceof Array){
var value = data.firstname.indexOf(key);
}
// you can use the last option in your code like this:
var firstnameHasKey = data.firstname instanceof Array && ~data.firstname.indexOf(key);
var lastnameHasKey = data.lastname instanceof Array && ~data.lastname.indexOf(key);
if(firstnameHasKey || lastnameHasKey){
// logics
}
If you test the instanceof && indexOf, there will never be an error. If firstname is undefined, the indexOf will never be checked.
Ofcourse you can use this for other types:
var myDate = new Date();
myDate instanceof Date; // returns true
myDate instanceof Object; // returns true
myDate instanceof String; // returns false
MDN documentation
I get return value as null and assign that value it shows as null in the UI. But I want to check some condition and if it is null, it should not show up anything..
I tried the below code and it doesn't work
var companyString;
if(utils.htmlEncode(item.companyname) == null)
{
companyString = '';
}
else{
companyString = utils.htmlEncode(item.companyname);
}
Compare item.companyname to null (but probably really any false-y value) - and not the encoded form.
This is because the encoding will turn null to "null" (or perhaps "", which are strings) and "null" == null (or any_string == null) is false.
Using the ternary operator it can be written as so:
var companyString = item.companyname
? utils.htmlEncode(item.companyname)
: "";
Or with coalescing:
var companyString = utils.htmlEncode(item.companyname ?? "");
Or in a long-hand form:
var companyString;
if(item.companyname) // if any truth-y value then encode
{
companyString = utils.htmlEncode(item.companyname);
}
else{ // else, default to an empty string
companyString = '';
}
var companyString;
if(item.companyname !=undefined && item.companyname != null ){
companyString = utils.htmlEncode(item.companyname);
}
else{
companyString = '';
}
Better to check not undefined along with not null in case of javascript. And you can also put alert or console.logto check what value you are getting to check why your if block not working. Also, utls.htmlEncode will convert your null to String having null literal , so compare without encoding.
var companyString="";
if(utils.htmlEncode(item.companyname) != null)
{
companyString = utils.htmlEncode(item.companyname);
}
Whats wrong with the below lines of code ...
Its keep complaing that systax error..at the like
if( (radioval === "undefined") || (radioval === null) || (radioval === "null") ) {
complete condition in action
if($('#digitallogin').is(":checked")){
var radioval = $("input[name=certificateradio]:checked").val();//its giving the string "user"
if( (radioval === "undefined") || (radioval === null) || (radioval === "null") ) { //this line
$("#login_error").text("please select the signin certificate");
return false;
}
}
Thanks for any Assistance.
There's no syntax error in your code. If the code had a syntax error, there would be an error message in the Firebug console (not the Watch panel), and the code would not run at all. You'd never get to your breakpoint.
The syntax errors in your screenshot are in your watch expressions. Delete the watch expressions and those error messages will go away.
In your updated screenshot I can see what the syntax error is. Take a close look at your watch expression (reformatted here to avoid scrolling):
if(
(radioval === "undefined") ||
(radioval === null) ||
(radioval === "null")
)
That's not an expression, it's an if statement. If you want to use it as a watch expression, you need just the expression inside the if statement. That is, remove the if( and the final ).
Regarding the specific tests you're making, jQuery's .val() method does return undefined if there are no elements selected. Note that there is an error in the jQuery documentation: it says that .val() returns null when there are no matching elements. This is incorrect; it returns undefined in this case. Here is the line of code in the jQuery source that returns undefined (because it is a simple return; with no value).
But that is the undefined value, not the string "undefined". You would test for this with radioval === undefined. Or you could use radioval == null. Note the == instead of ===; this test matches both null and undefined values. That may be the safest bet, in case the jQuery team ever decides to change the code to match the documentation and start returning null instead of undefined. (An unlikely possibility, but you never know.) By testing radioval == null it would test against either value.
Try
if( (typeof radioval === "undefined") || (radioval === null) || (radioval == "null") ) {
in the third comparison of radioval
radioval == "null" and not === null
$("input[name=certificateradio]:checked").val(); returns undefined (typeof undefined === undefined) if it's unchecked or "on" (typeof "on" === string) if it's checked.
a small example:
<input name="certificateradio" type="checkbox" />
<button>run</button>
$("button").click(function(){
console.log($("input[name=certificateradio]:checked").val());
console.log(typeof $("input[name=certificateradio]:checked").val());
});
http://jsfiddle.net/x2uw4/1/
try to use:
if($("input[name=certificateradio]:checked").val() ==="on"){
...
}
.val() returns a string value, null or an Array in case of a multiselect value, so you could try:
if (/null|undefined/i.test(radioval) || !radioval.length){ /*...*/ }
Since you seem to check for [one of] $("input[name=certificateradio]") being checked, it may also be sufficient to do
if( ( !$("input[name=certificateradio]:checked").length ) {
$("#login_error").text("please select the signin certificate");
return false;
}
Or even shorter
if( $('#digitallogin').is(":checked") &&
!$("input[name=certificateradio]:checked").length ) {
$("#login_error").text("please select the signin certificate");
return false;
}
Is there a better way for checking an attribute for:
it exist. so value must be false if attribute doesn't exist
Value is correct (boolean)
var isOwner = false;
if ($(selectedItem).is('[data-isOwner="True"]') || $(selectedItem).is('[data-isOwner="true"]')) {
isOwner = true;
} else {
isOwner = false;
}
Now I need to check for 'True' and 'true'...
Thanks
You can convert the value stored in data-isOwner to lower case and only compare the value to 'true'.
if (($(selectedItem).attr ('data-isOwner') || '').toLowerCase () == 'true')
The above use of <wanted-value> || '' will make it so that if the selectedItem doesn't have the attribute data-isOwner the expression will result in an empty string, on which you can call toLowerCase without errors.
Without this little hack you'd have to manually check so that the attribute is indeed present, otherwise you'd run into a runtime-error when trying to call toLowerCase on an undefined object.
If you find the previously mentioned solution confusing you could use something as
var attr_value = $(selectedItem).attr ('data-isOwner');
if (typeof(attr_value) == 'string' && attr_value.toLowerCase () == 'true') {
...
}
here's my function for checking zipcode. When a null values comes in, i keep getting "Object Required" Does anyone know where im going wrong?
aspx tags -
asp:CustomValidator
ID="cv_zipcode"
runat="server"
ControlToValidate="tb_zipcode"
ClientValidationFunction="ValidateZipcode"
ValidateEmptyText="true"
Display="Dynamic"
ValidationGroup="vgroup">
</asp:CustomValidator>
function ValidateZipcode(sender, args) {
var regZipcode = '\d{5}'
var zipcode = document.getElementById(sender.id.replace(/cv_/, "tb_"));
if ((zipcode.value == "ZipCode") || (zipcode.value.length == null) || (zipcode.value.length == "")) {
zipcode.style.backgroundColor = "#f6e086";
args.IsValid = false; return;
} else {
args.IsValid = true;
zipcode.style.backgroundColor = "white";
}
}
I'm not sure exactly which value is null, but in general, if you have a variable x which may or may not be null, and you want to do something with x, you can do the following:
x != null && do_something_with(x)
If x == null, then this returns false and doesn't try to execute do_something_with(). Otherwise, this expression returns the value of do_something_with(x).
If you just do_something_with(x), and x is null, and do_something_with() is not expecting a null, you can get errors.
EDIT:
try:
if ((zipcode == null) || (zipcode.value == null) || [everything else])
zipcode.value.length returns an integer
I think you should have
if ((zipcode.value == "ZipCode") || (zipcode.value.length == 0))
I would be a little suspect of this line:
if ((zipcode.value == "ZipCode") || (zipcode.value.length == null) || (zipcode.value.length == "")) {
Try this instead:
if ((zipCode.value == null) || (zipcode.value== "") || (zipcode.value.length == 0)) {
That error message usually indicates that you've tried to get or set a property or call a method of something that isn't an object, which tends to happen when a variable that you thought referred to an object is actuall null or undefined. That is, if someVariable is null or undefined then you can't say someVariable.someProperty.
If .getElementById() doesn't find a matching element it returns null, so in this line:
var zipcode = document.getElementById(sender.id.replace(/cv_/, "tb_"));
zipcode is potentially set to null, and if it is then all attempts to access properties of zipcode like zipcode.value and zipcode.style will fail.
If the parameter args comes in as null or undefined then attempting to set args.IsValid will fail, and similarly if the parameter sender is null or undefined then sender.id will fail.
So, if you have a variable that might be null you should test that before trying to do anything else with it.