checking for username in database exists or not - asp.net

I want that if a user enters a username and as soon as he clicks tab or goes to the next option, a validation should be displayed whether the username is available or does not exist in the database. I don't want to use the functionality of the button of check availablity.

http://www.aspdotnet-suresh.com/2011/03/how-to-check-username-availability.html this might help it uses ajax .

Create a simple webservice that takes string as an argument and returns true/false as reply based on the availability of the username.
Now create a javascript function and call this webservice through the javascript function.
Call this javascript function on the "onchange" event of the input element(textbox) where user is entering the username.
Put a span element in-front of the textbox(input) and show relevant message based on the availability of the username.
That's it.
You can find the code on the following URL:
validate username ajax and json and asp.net

1st make textbox is autopostback =true
2nd go to events of the text box and go to textchanges event that will fire as soon as you make tab or textbox unfocused
3rd write the code that get the username from db and check exist or not from here
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
//get user name from db
}

Related

I'm having trouble understanding page validation

Okay, so I have my asp.net page with all of my comparison and requiredfield validators. This leaves me with two concerns.
What additional validation do I need? Do I need anything in the code behind? I want them to be unable to hit the 'save' button until their textbox information is complete, and it seems to be doing this with just the validator controls, but I'm unsure if there are other steps I need to take.
If I have a requiredfield validator and I want to turn it off under special circumstances, where in the codebehind would I set it to true? Can I do it on the 'save' button click, before it prevents the button from functioning?
1.
You need as many validations as necessary, you can create many different validators.
You need server-side validation in the code behind. If a postback occurred then the form passed the validation, but there are some validation features which are unusable at client-side. For instance you register to a homepage and you have a form where the username is required and there is a regular expression validator too. These validators will run at client-side. But if the username has to be unique and you can only check that using a database then obviously this can't be checked at the client-side, therefore, the client-side will evaluate the page to be valid, a postback will occur and it's the job of the server-side to check whether the username is unique.
Note that you can create custom validators if you need to do anything exotic.
2.
Depending on your needs you can set the Enabled property of your validators whenever you need to do that. Read more about that here.
As per my knowledge,
We should include a check for "Page.IsValid" on the server side (code behind) whenever we are using the ASP.NET Validators. This would ensure a check at the server side even if the javascript is being disabled on the browser.
No, you can't do that on the save button click as the button click would not be hit until the validation passes.
Hope this Helps!!
1 if you want add validation server
protected void Button1_Click(object sender, EventArgs e) {
//Proceed only if the validation is successfull
if (!Page.IsValid) {
return;}
}
2 You can set CausesValidation="false" to button
The #1 question with Page.IsValid is ok.
The requiredfieldvalidator is a javascript validator, so you can't disable it from codebehind once it was enabled (unless it satisfies the condition and you can go through). But it's possible to disable it via javascript, check this code:
ValidatorEnable(workPhoneValidator, false);
Link: Dynamically enable or disable RequiredFieldValidator based on value of DropDownList

Can you do a Form submission with Javascript disable

If a user has disabled Javascript in their client browser and they try to use a Form on the Web Page, this causes a Postback. My understanding is that the Form would create a Javascript function __doPostBack to handle the Form submission, but with Javascript disabled a normal Postback occurs. When checking what as been posted back to the server, the Form is empty. Can a Form work with Javascript disabled?
A form ABSOLUTELY works without Javascript.
Just put your form consumption code in the button click event.
something like this perhaps
void btnSubmit_Click(Object sender,
EventArgs e)
{
// When the button is clicked,
// send the values of the input fields to the database.
}
The form will be submitted without javascript.
Any client-side validation will not run, so you may have invalid data. This is why you always, always perform server-side validation of your forms.
Now, you state that your form is empty. If you depend on javascript, jquery, or some other client-side programming to set values in your fields, then you may see empty values. I'm not sure where you're checking, but if you take a look at the request object in your page_load, you should see your data there.

ASP CompareValidator and NOT submitting form

I have a textbox and a submit button. The textbox is a date-entry field.
Attached to it, I have a compare validator with the type set to "date". It does validate and show an error message.
Problem is, the user can still click on the submit button. I'd like to prevent that. If the user has entered something like 03/hello/2011, he or she should not be able to submit the form.
How can I accomplish this?
Any ideas?
Thanks,
Jason
Associate the validator and the submit button in a single validation group. Both of them have the property validation group. Provide a name say pageValidation to both the control's property.
I've encountered this problem myself, that a page with validation errors can still continue on to submission when the user clicks the submit button.
What you can do is something like this :
protected void submitClicked(object sender, EventArgs e)
{
if (!Page.IsValid)
{
// somehow the user was able to submit their form even though there are
// validation errors. Stop here and let ASP.NET present the error messages
// to the user
return;
}
// do submission stuff here like putting things in the database
}

Confirm message in ASP.NET

I wrote this statment in my code:
Response.Write("<script language=javascript>confirm('The system not allow negative inventory,continue?');</script>");
how can I handel if the user clicked "Ok" or "Cancel" button?
You should put this confirm to your submit button like that :
btnSubmit.Attributes["onclick"] +=
"return confirm('The system not allow negative inventory,continue?');"
If user click cancel, your page won't be postback.
But if you ask you can determine user's action at server side, the answer is no, not directly. You should add some trick, to get the user's action. Maybe you should set the user's action into a hidden field and at server side get this value and continue.
Restricting my answer to Javascript and not how the result of your code would interact with the flow of the page, the Javascript confirm method returns the value of the user's selected option:
var result = confirm('The system does not allow negative inventory. Continue?');
if (result == true)
// The user wants to continue. Proceed accordingly.
else
// The user does not want to continue.
You can use the value to branch your logic accordingly.
That code will execute on the client side. To know if the user clicked OK or cancel on the server, you will need to make your script send a request back to the server. You can't handle the dialog in the same request, because your code will have ended before the user sees anything in their browser.
You could output JavaScript like this:
location.href = 'Handler.aspx?confirmed=' + confirm('Do you want to do X?');
This will send the browser to either Handler.aspx?confirmed=true or Handler.aspx?confirmed=false.
On the onClientClick tag (in the aspx file) u can write
confirm('your message');
and onClick tag reference it to function in code-behind where you can write the function which will only be executed on OK click of the messagebox.
Using a javascript confirm function it is possible to allow a user to confirm or canel a button click. If the user presses yes on the message/alert box the button click even will be triggered on the server. If the no button is click no event will be triggered. This is especially useful with buttons used for deleting.
button.Attributes("onclick") = "javascript:return " & _
"confirm('Are You Sure you want to do this operation?') "

Firing Postback in ASP.NET Without javascript

Is this possible?
EDIT
I want to user the asp:ListView
List item
I want to use it for editing
I dont want the postback to use Javascript when I put it into the edit mode
I've been trying to use a to do this instead of a link button but to no avail.
.
The only way to trigger a PostBack without JavaScript is by using a submit button. This is an HTML limitation, not an ASP.NET one.
I'm a little unsure of what you're trying to do without more detail. If I knew more specifically what you're trying to accomplish I could give you more details.
Like you mentioned in your post, you could use the <a> tag to send the user to the page you want.
You can add information to the link like so:
Click here for a new item
Then in the page load of newpage.aspx you can check what the action the user selected was, the query parameters are stored in the Request (Language is C#).
protected void Page_Load(object sender, EventArgs e)
{
string action = Request.Params["action"];
if(!String.IsNullOrEmpty(action))
{
switch(action)
{
case "newitem":
//handle the new item action
break;
case "deleteitem":
//handle the delete item action
break;
//handle other actions.
}
}
}
EDIT: You should be aware that the <a> tag will send the user to the page specified, the page though will act as if it is the first time the user has visited the page. With that said, the page variable IsPostBack will be false.

Resources