Click here to Skip to main content
16,018,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hii

i am able to enter charecters and digits, if i enter both charecters and digits then only the form should submit otherwise it shouldn't. suppose if i enter complete charecters or if i enter complete digits the form should not submit.....if i enter both only the form should submit in windows based applications


Thank you
Posted
Comments
[no name] 4-Oct-12 7:45am    
If it was so "urgent" then you should have taken the time to clearly define what it is that you are trying to do and included the code that you are having trouble with. "enter complete charecters or if i enter complete digits the form should not submit", is complete nonsense. How are you entering incomplete characters or digits?
mahesh_chs 5-Oct-12 5:58am    
hii thanks OriginalGriff
the problem was solved

Something I learned from a recent answer I gave is you can use zero-width negative lookahead to introduce assertion into your regular expressions.

Try this regular expression:

(?!^[a-zA-Z]+$)(?!^[0-9]+$)^(?:[a-zA-Z0-9]+)$


For an explanation see this answer: http://www.codeproject.com/script/Answers/Post.aspx?aid=469314[^].

The regular expression above will forbid strings being made of only digits or only of characters, but will gladly accept a mixture of both.
In my previous solution I also had to use quantifiers, but you've not mentioned any limits on the length of the validated string, so I left it out. You can easily see how this is done from the solution I pointed you to.

Regards,

— Manfred
 
Share this answer
 
v2
Use the keydown event with two bool values one for numeric and one for alphabets, with the help of key down or key press event assign value true if you get number and alphabets. while clicking on submit button check that you have true values of bool variables if not the ask user to enter number and alphabets both.

Thanks,
Ambesha
 
Share this answer
 
I would create two regexes: one for any letter, one for any digit.
Only if both match does it validate.
C#
string instr = "abc123";
Regex letters = new Regex("[a-zA-Z]");
Regex numbers = new Regex("[0-9]");
if (letters.IsMatch(instr) && numbers.IsMatch(instr))
    {
    ...
    }
 
Share this answer
 
Comments
mahesh_chs 5-Oct-12 5:58am    
Thanks
Handle the one of KeyDown / keyPress events of the textbox and validate the text for numbers and text together.
If both of them are not present, don't enable the submit button.
 
Share this answer
 
Comments
fjdiewornncalwe 4-Oct-12 10:01am    
Simple and effective.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900