Check Whether a TextBox is empty or not
You should use String.IsNullOrEmpty()
to make sure it is neither empty nor null (somehow):
if (String.IsNullOrEmpty(textBox1.Text))
{
// Do something...
}
More examples here.
For practical purposes you might also consider using String.IsNullOrWhitespace()
since a TextBox expecting whitespace as input probably negates any purpose, except in case of, say, letting the user pick a custom separator for stuff.
How to check for empty textbox
If you want to check empty for all text box control in your page .Try IsNullOrWhiteSpace
foreach (Control child in this.Controls)
{
TextBox textBox = child as TextBox;
if (textBox != null)
{
if (!string.IsNullOrWhiteSpace(textBox.Text))
{
MessageBox.Show("Text box can't be empty");
}
}
}
Check if textbox is empty - is there a better way?
Not really - however, you could make a little extension if it would save you time:
public static class TextBoxExtensions
{
public static bool IsEmpty(this TextBox textBox)
{
return string.IsNullOrWhiteSpace(null == textBox ? null : textBox.Text);
}
}
usage:
if(TextBox1.IsEmpty())
{
....
Checking textbox if it's empty in Javascript
It should be this:
var chatinput = document.getElementById("chatinput").value;
if (chatinput == "" || chatinput.length == 0 || chatinput == null)
{
// Invalid... Box is empty
}
Or shorthanded:
if (!document.getElementById("chatinput").value)
{
// Invalid... Box is empty
}
The =
assigns a value whereas ==
checks whether the values are equal.
Checking if text boxes are empty
You can try to loop at your container (ie form, panel, groupbox) for each textbox without content, then increment whenever there is a textbox that doesn't have content.
Example:
Dim checkr as integer = 0
Dim this As Control
For Each this In that.Controls
If TypeOf this Is TextBox Then
If this.text = "" then
Checkr += 1
End if
End If
Next
If checkr > 0 then
msgbox("Cannot proceed because a textbox has no content")
Else
'......(what you were gonna do)
End If
checking to see if a text box is empty in php
<?php
include "connect.php";
if(isset($_POST["submit"]))
{
$ownerName = isset($_POST['OwnerName']) ? $_POST['OwnerName']: '';
$location = isset($_POST['Location']) ? $_POST['Location']: '';
$phoneNumber = isset($_POST['PhoneNumber']) ? $_POST['PhoneNumber']: '';
//preventing sql injection
/*
$ownerName = mysqli_real_escape_string($con, $ownerName);
$$location = mysqli_real_escape_string($con, $ownerName);
$$phoneNumber = mysqli_real_escape_string($con, $ownerName);
*/
if(empty($ownerName) || empty($location) || empty($phoneNumber)){
echo("Missing Information!");
/*if(is_numeric($phoneNumber) ){
settype($phoneNumber, "integer");
}*/
}else{
$query = "INSERT INTO OWNER VALUES ('$ownerName','$location', $phoneNumber)";
if (!mysqli_query($con,$query))
{
die('Error: ' . mysqli_error($con));
}
echo "<script type='text/javascript'>alert('1 record added')</script>";
}
mysqli_close($con);
?>
Also try using var_dump($_POST) to see what is sent.
EDIT:
I've updated if/else statement so now it should work. Also, make sure you have your error reporting enabled:
<?php
// Turn off error reporting
error_reporting(0);
// Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Report all errors
error_reporting(E_ALL);
// Same as error_reporting(E_ALL);
ini_set("error_reporting", E_ALL);
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
?>
Checking if a textbox is empty in Javascript
onchange
will work only if the value of the textbox changed compared to the value it had before, so for the first time it won't work because the state didn't change.
So it is better to use onblur
event or on submitting the form.
function checkTextField(field) { document.getElementById("error").innerText = (field.value === "") ? "Field is empty." : "Field is filled.";}
<input type="text" onblur="checkTextField(this);" /><p id="error"></p>
Related Topics
Which Operating Systems Support Native (Inotify-Like) File Watching in Java
Why Does Killing Jvm Also Terminates Its Child Process If Waitfor Has Been Used
Java's 'Tnameserv' Takes 3+ Minutes to Be "Ready", Why
How to Set Classpath in Linux to Let Java Find Jar File
Webdriver for Firefox: Browser Starts W/ Empty Page, Hangs for 2 Min, Restarts, Then Test Runs. Why
How to Get Java to Use the Serial Port in Linux
How to Use Linux Shared Libraries in Java
Commons Vfs and Java.Net.Url - Adding Support for "Sftp://" Protocol
Local_Policy.Jar and Us_Export_Policy.Jar Different with Unlimited Strength VS Default
Create a Shell Script to Run a Java Program on Linux
Find Native Memory Leak in Java Application Using Jemalloc
Decibel Values at Specific Points in Wav File
Variable Column Names Using Prepared Statements
Connection Pooling Options With Jdbc: Dbcp VS C3P0
How to Check the Jdk Version Used to Compile a .Class File
Remote Debugging a Java Application
What Would Cause a Java Process to Greatly Exceed the Xmx or Xss Limit